|
16 | 16 | import static org.junit.jupiter.api.Assertions.assertTrue; |
17 | 17 | import org.junit.jupiter.api.BeforeEach; |
18 | 18 | import org.junit.jupiter.api.DisplayName; |
| 19 | +import org.junit.jupiter.api.Nested; |
19 | 20 | import org.junit.jupiter.api.Test; |
20 | 21 | import org.junit.jupiter.api.io.TempDir; |
21 | 22 |
|
|
29 | 30 | import service.SessionService; |
30 | 31 | import service.TestService; |
31 | 32 | import service.VisualizationService; |
| 33 | +import viewmodel.TestResultViewModel; |
32 | 34 |
|
33 | 35 | /** |
34 | 36 | * Integration tests for AutomatonController. |
@@ -359,6 +361,175 @@ void defaultConstructor_works() { |
359 | 361 | assertNotNull(dfa); |
360 | 362 | } |
361 | 363 |
|
| 364 | + // ═══════════════════════════════════════════════════════════════════ |
| 365 | + // ViewModel Test Execution Tests |
| 366 | + // ═══════════════════════════════════════════════════════════════════ |
| 367 | + |
| 368 | + @Nested |
| 369 | + @DisplayName("ViewModel Test Execution") |
| 370 | + class ViewModelTestExecutionTests { |
| 371 | + |
| 372 | + // Valid CFG definition with 8 rules (S -> aB | bA, A -> a | aS | bAA, B -> b | bS | aBB) |
| 373 | + private static final String CFG_WITH_MANY_RULES = |
| 374 | + "Variables = S A B\n" + |
| 375 | + "Terminals = a b\n" + |
| 376 | + "Start = S\n" + |
| 377 | + "\n" + |
| 378 | + "S -> a B | b A\n" + |
| 379 | + "A -> a | a S | b A A\n" + |
| 380 | + "B -> b | b S | a B B\n"; |
| 381 | + |
| 382 | + // Valid PDA with 5 transitions |
| 383 | + private static final String PDA_WITH_TRANSITIONS = |
| 384 | + "states: q0 q1 q2 q3\n" + |
| 385 | + "alphabet: a b\n" + |
| 386 | + "stack_alphabet: a Z\n" + |
| 387 | + "start: q0\n" + |
| 388 | + "stack_start: Z\n" + |
| 389 | + "finals: q3\n" + |
| 390 | + "transitions:\n" + |
| 391 | + "q0 a Z -> q1 aZ\n" + |
| 392 | + "q0 a a -> q1 aa\n" + |
| 393 | + "q1 b a -> q2 eps\n" + |
| 394 | + "q2 b a -> q2 eps\n" + |
| 395 | + "q2 eps Z -> q3 eps\n"; |
| 396 | + |
| 397 | + @Test |
| 398 | + @DisplayName("runTestsWithViewModel returns success for DFA") |
| 399 | + void testRunTestsWithViewModel_DFASuccess() throws Exception { |
| 400 | + Automaton dfa = controller.createAutomaton(MachineType.DFA); |
| 401 | + controller.parse(dfa, VALID_DFA); |
| 402 | + |
| 403 | + // Create test file with CSV format: input,expected (1=accept, 0=reject) |
| 404 | + File testFile = tempDir.resolve("test.test").toFile(); |
| 405 | + writeFile(testFile, "0,1\n1,0\n"); |
| 406 | + |
| 407 | + TestResultViewModel result = controller.runTestsWithViewModel(dfa, testFile.getAbsolutePath(), null); |
| 408 | + |
| 409 | + assertNotNull(result); |
| 410 | + assertFalse(result.hasLimitViolation()); |
| 411 | + assertEquals(2, result.getTotalTests()); |
| 412 | + } |
| 413 | + |
| 414 | + @Test |
| 415 | + @DisplayName("runTestsWithViewModel returns CFG violation when rules exceed limit") |
| 416 | + void testRunTestsWithViewModel_CFGViolation() throws Exception { |
| 417 | + Automaton cfg = controller.createAutomaton(MachineType.CFG); |
| 418 | + controller.parse(cfg, CFG_WITH_MANY_RULES); |
| 419 | + |
| 420 | + // Create test file |
| 421 | + File testFile = tempDir.resolve("test.test").toFile(); |
| 422 | + writeFile(testFile, "ab,1\n"); |
| 423 | + |
| 424 | + // Set settings with max 3 rules (CFG has 8) |
| 425 | + controller.setTestSettings(new SessionService.TestSettings(0, 100, 30, 3, null, null)); |
| 426 | + |
| 427 | + TestResultViewModel result = controller.runTestsWithViewModel(cfg, testFile.getAbsolutePath(), null); |
| 428 | + |
| 429 | + assertNotNull(result); |
| 430 | + assertTrue(result.hasLimitViolation()); |
| 431 | + assertEquals("CFG_RULES", result.getLimitViolationType()); |
| 432 | + assertEquals(0.0, result.getEarnedPoints()); |
| 433 | + } |
| 434 | + |
| 435 | + @Test |
| 436 | + @DisplayName("runTestsWithViewModel returns PDA violation when transitions exceed limit") |
| 437 | + void testRunTestsWithViewModel_PDAViolation() throws Exception { |
| 438 | + Automaton pda = controller.createAutomaton(MachineType.PDA); |
| 439 | + controller.parse(pda, PDA_WITH_TRANSITIONS); |
| 440 | + |
| 441 | + // Create test file |
| 442 | + File testFile = tempDir.resolve("test.test").toFile(); |
| 443 | + writeFile(testFile, "ab,1\n"); |
| 444 | + |
| 445 | + // Set settings with max 2 transitions (PDA has 5) |
| 446 | + controller.setTestSettings(new SessionService.TestSettings(0, 100, 30, null, 2, null)); |
| 447 | + |
| 448 | + TestResultViewModel result = controller.runTestsWithViewModel(pda, testFile.getAbsolutePath(), null); |
| 449 | + |
| 450 | + assertNotNull(result); |
| 451 | + assertTrue(result.hasLimitViolation()); |
| 452 | + assertEquals("PDA_TRANSITIONS", result.getLimitViolationType()); |
| 453 | + assertEquals(0.0, result.getEarnedPoints()); |
| 454 | + } |
| 455 | + } |
| 456 | + |
| 457 | + // ═══════════════════════════════════════════════════════════════════ |
| 458 | + // Limit Validation Tests |
| 459 | + // ═══════════════════════════════════════════════════════════════════ |
| 460 | + |
| 461 | + @Nested |
| 462 | + @DisplayName("Limit Validation") |
| 463 | + class LimitValidationTests { |
| 464 | + |
| 465 | + @Test |
| 466 | + @DisplayName("validateLimits returns CFG violation when rules exceed limit") |
| 467 | + void testValidateLimits_CFGViolation() { |
| 468 | + String cfgInput = |
| 469 | + "Variables = S A B\n" + |
| 470 | + "Terminals = a b\n" + |
| 471 | + "Start = S\n" + |
| 472 | + "\n" + |
| 473 | + "S -> a B | b A\n" + |
| 474 | + "A -> a | a S | b A A\n" + |
| 475 | + "B -> b | b S | a B B\n"; |
| 476 | + |
| 477 | + Automaton cfg = controller.createAutomaton(MachineType.CFG); |
| 478 | + controller.parse(cfg, cfgInput); |
| 479 | + |
| 480 | + // Set settings with max 3 rules (CFG has 8) |
| 481 | + controller.setTestSettings(new SessionService.TestSettings(0, 100, 30, 3, null, null)); |
| 482 | + |
| 483 | + ValidationMessage violation = controller.validateLimits(cfg); |
| 484 | + |
| 485 | + assertNotNull(violation); |
| 486 | + assertTrue(violation.getMessage().contains("CFG")); |
| 487 | + } |
| 488 | + |
| 489 | + @Test |
| 490 | + @DisplayName("validateLimits returns PDA violation when transitions exceed limit") |
| 491 | + void testValidateLimits_PDAViolation() { |
| 492 | + String pdaInput = |
| 493 | + "states: q0 q1 q2 q3\n" + |
| 494 | + "alphabet: a b\n" + |
| 495 | + "stack_alphabet: a Z\n" + |
| 496 | + "start: q0\n" + |
| 497 | + "stack_start: Z\n" + |
| 498 | + "finals: q3\n" + |
| 499 | + "transitions:\n" + |
| 500 | + "q0 a Z -> q1 aZ\n" + |
| 501 | + "q0 a a -> q1 aa\n" + |
| 502 | + "q1 b a -> q2 eps\n" + |
| 503 | + "q2 b a -> q2 eps\n" + |
| 504 | + "q2 eps Z -> q3 eps\n"; |
| 505 | + |
| 506 | + Automaton pda = controller.createAutomaton(MachineType.PDA); |
| 507 | + controller.parse(pda, pdaInput); |
| 508 | + |
| 509 | + // Set settings with max 2 transitions (PDA has 5) |
| 510 | + controller.setTestSettings(new SessionService.TestSettings(0, 100, 30, null, 2, null)); |
| 511 | + |
| 512 | + ValidationMessage violation = controller.validateLimits(pda); |
| 513 | + |
| 514 | + assertNotNull(violation); |
| 515 | + assertTrue(violation.getMessage().contains("PDA")); |
| 516 | + } |
| 517 | + |
| 518 | + @Test |
| 519 | + @DisplayName("validateLimits returns null when within limits") |
| 520 | + void testValidateLimits_NoViolation() { |
| 521 | + Automaton dfa = controller.createAutomaton(MachineType.DFA); |
| 522 | + controller.parse(dfa, VALID_DFA); |
| 523 | + |
| 524 | + // Set permissive settings |
| 525 | + controller.setTestSettings(new SessionService.TestSettings(0, 100, 30, 100, 100, null)); |
| 526 | + |
| 527 | + ValidationMessage violation = controller.validateLimits(dfa); |
| 528 | + |
| 529 | + assertNull(violation); |
| 530 | + } |
| 531 | + } |
| 532 | + |
362 | 533 | // ═══════════════════════════════════════════════════════════════════ |
363 | 534 | // Helper Methods |
364 | 535 | // ═══════════════════════════════════════════════════════════════════ |
|
0 commit comments