|
| 1 | +import qtil.results.LimitResults |
| 2 | +import qtil.testing.Qnit |
| 3 | +import Bugs |
| 4 | + |
| 5 | +module TestConfig implements LimitResultsConfigSig<Bug, BugField> { |
| 6 | + predicate problem(Bug bug, BugField field) { field.getBug() = bug } |
| 7 | + |
| 8 | + bindingset[remaining] |
| 9 | + string message(Bug bug, BugField field, string remaining) { |
| 10 | + result = bug.getName() + " has field " + field.getFieldName() + remaining |
| 11 | + } |
| 12 | + |
| 13 | + string orderBy(BugField field) { result = field.getFieldName() } |
| 14 | + |
| 15 | + int maxResults() { result = 3 } |
| 16 | +} |
| 17 | + |
| 18 | +module Results = LimitResults<Bug, BugField, TestConfig>; |
| 19 | + |
| 20 | +/** BugA has 1 field (X), which is fewer than maxResults=3, so all results are shown. */ |
| 21 | +class TestBugAResultCount extends Test, Case { |
| 22 | + override predicate run(Qnit test) { |
| 23 | + if count(BugField f, string msg | Results::hasLimitedResult(any(Bug b | b = TBugA()), f, msg)) = |
| 24 | + 1 |
| 25 | + then test.pass("BugA: 1 result shown (fewer than maxResults)") |
| 26 | + else test.fail("BugA: wrong result count") |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +/** BugA shows field X with no suffix since total <= maxResults. */ |
| 31 | +class TestBugANoSuffix extends Test, Case { |
| 32 | + override predicate run(Qnit test) { |
| 33 | + if |
| 34 | + exists(BugField f, string msg | |
| 35 | + Results::hasLimitedResult(any(Bug b | b = TBugA()), f, msg) and |
| 36 | + f.getFieldName() = "X" and |
| 37 | + msg = "BugA has field X" |
| 38 | + ) |
| 39 | + then test.pass("BugA: correct message with no suffix") |
| 40 | + else test.fail("BugA: message incorrect") |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +/** BugB has 3 fields (A, B, C), exactly maxResults=3, so all results are shown. */ |
| 45 | +class TestBugBResultCount extends Test, Case { |
| 46 | + override predicate run(Qnit test) { |
| 47 | + if count(BugField f, string msg | Results::hasLimitedResult(any(Bug b | b = TBugB()), f, msg)) = |
| 48 | + 3 |
| 49 | + then test.pass("BugB: 3 results shown (exactly maxResults)") |
| 50 | + else test.fail("BugB: wrong result count") |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +/** BugB shows all 3 fields with no suffix since total <= maxResults. */ |
| 55 | +class TestBugBNoSuffix extends Test, Case { |
| 56 | + override predicate run(Qnit test) { |
| 57 | + if |
| 58 | + forall(string fieldName | |
| 59 | + fieldName = ["A", "B", "C"] and |
| 60 | + exists(BugField f | |
| 61 | + f.getFieldName() = fieldName and |
| 62 | + f.getBugName() = "BugB" |
| 63 | + ) |
| 64 | + | |
| 65 | + exists(BugField f, string msg | |
| 66 | + Results::hasLimitedResult(any(Bug b | b = TBugB()), f, msg) and |
| 67 | + f.getFieldName() = fieldName and |
| 68 | + msg = "BugB has field " + fieldName |
| 69 | + ) |
| 70 | + ) |
| 71 | + then test.pass("BugB: all 3 results shown with no suffix") |
| 72 | + else test.fail("BugB: some results have wrong message or are missing") |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +/** BugC has 5 fields (A, B, C, D, E), which exceeds maxResults=3, so only 3 are shown. */ |
| 77 | +class TestBugCResultCount extends Test, Case { |
| 78 | + override predicate run(Qnit test) { |
| 79 | + if count(BugField f, string msg | Results::hasLimitedResult(any(Bug b | b = TBugC()), f, msg)) = |
| 80 | + 3 |
| 81 | + then test.pass("BugC: 3 results shown (capped at maxResults)") |
| 82 | + else test.fail("BugC: wrong result count") |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +/** BugC shows only the first 3 fields alphabetically (A, B, C), not D or E. */ |
| 87 | +class TestBugCTopRanked extends Test, Case { |
| 88 | + override predicate run(Qnit test) { |
| 89 | + if |
| 90 | + forall(string fieldName | fieldName = ["A", "B", "C"] | |
| 91 | + exists(BugField f | |
| 92 | + Results::hasLimitedResult(any(Bug b | b = TBugC()), f, _) and |
| 93 | + f.getFieldName() = fieldName and |
| 94 | + f.getBugName() = "BugC" |
| 95 | + ) |
| 96 | + ) and |
| 97 | + not exists(BugField f | |
| 98 | + Results::hasLimitedResult(any(Bug b | b = TBugC()), f, _) and |
| 99 | + f.getFieldName() = ["D", "E"] and |
| 100 | + f.getBugName() = "BugC" |
| 101 | + ) |
| 102 | + then test.pass("BugC: top 3 alphabetical fields shown, D and E omitted") |
| 103 | + else test.fail("BugC: wrong fields shown") |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +/** BugC shows a suffix " (and 2 more)" since 5 - 3 = 2 entities are omitted. */ |
| 108 | +class TestBugCSuffix extends Test, Case { |
| 109 | + override predicate run(Qnit test) { |
| 110 | + if |
| 111 | + forall(BugField f, string msg | |
| 112 | + Results::hasLimitedResult(any(Bug b | b = TBugC()), f, msg) |
| 113 | + | |
| 114 | + msg.matches("% (and 2 more)") |
| 115 | + ) |
| 116 | + then test.pass("BugC: all shown results have ' (and 2 more)' suffix") |
| 117 | + else test.fail("BugC: some results have wrong suffix") |
| 118 | + } |
| 119 | +} |
0 commit comments