|
| 1 | +package text |
| 2 | + |
| 3 | +import "testing" |
| 4 | + |
| 5 | +func getTestTrie() *FilterTrie { |
| 6 | + trie := &FilterTrie{} |
| 7 | + trie.Put("shit", "horseshit", "bullshit") |
| 8 | + trie.Put("fuck") |
| 9 | + return trie |
| 10 | +} |
| 11 | + |
| 12 | +func TestPositiveProfanities(t *testing.T) { |
| 13 | + trie := getTestTrie() |
| 14 | + texts := map[string]string{ |
| 15 | + "this shit": "shit", |
| 16 | + "fuck this": "fuck", |
| 17 | + "hello world": "", |
| 18 | + } |
| 19 | + |
| 20 | + for text, expected := range texts { |
| 21 | + result := trie.Test(text) |
| 22 | + if result == nil && len(expected) != 0 { |
| 23 | + t.Errorf("Expected '%s' but got nil from '%s'", expected, text) |
| 24 | + } else if result != nil && expected != *result { |
| 25 | + t.Errorf("Expected '%s' but got '%s' from '%s'", expected, *result, text) |
| 26 | + } |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +func TestNegativeProfanities(t *testing.T) { |
| 31 | + trie := getTestTrie() |
| 32 | + texts := map[string]string{ |
| 33 | + "this horseshit": "", |
| 34 | + "fuck this bullshit": "fuck", |
| 35 | + "hello world": "", |
| 36 | + } |
| 37 | + |
| 38 | + for text, expected := range texts { |
| 39 | + result := trie.Test(text) |
| 40 | + if result == nil && len(expected) != 0 { |
| 41 | + t.Errorf("Expected '%s' but got nil from '%s'", expected, text) |
| 42 | + } else if result != nil && expected != *result { |
| 43 | + t.Errorf("Expected '%s' but got '%s' from '%s'", expected, *result, text) |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +func TestLongText(t *testing.T) { |
| 49 | + trie := getTestTrie() |
| 50 | + // an excerpt from romeo and juliet, copyright: public domain |
| 51 | + text := ` |
| 52 | + PRINCE. |
| 53 | + Rebellious subjects, enemies to peace, |
| 54 | + Profaners of this neighbour-stained steel,— |
| 55 | + Will they not hear? What, ho! You men, you beasts, |
| 56 | + That quench the fire of your pernicious rage |
| 57 | + With purple fountains issuing from your veins, |
| 58 | + On pain of torture, from those bloody hands |
| 59 | + Throw your mistemper’d weapons to the ground |
| 60 | + And hear the sentence of your moved prince. |
| 61 | + Three civil brawls, bred of an airy word, |
| 62 | + By thee, old Capulet, and Montague, |
| 63 | + Have thrice disturb’d the quiet of our streets, |
| 64 | + And made Verona’s ancient citizens |
| 65 | + Cast by their grave beseeming ornaments, |
| 66 | + To wield old partisans, in hands as old, |
| 67 | + Canker’d with peace, to part your canker’d hate. |
| 68 | + If ever you disturb our streets again, |
| 69 | + Your lives shall pay the forfeit of the peace. |
| 70 | + For this time all the rest depart away: |
| 71 | + You, Capulet, shall go along with me, |
| 72 | + And Montague, come you this afternoon, |
| 73 | + To know our farther pleasure in this case, |
| 74 | + To old Free-town, our common judgement-place. |
| 75 | + Once more, on pain of death, all men depart. |
| 76 | + |
| 77 | + [_Exeunt Prince and Attendants; Capulet, Lady Capulet, Tybalt, |
| 78 | + Citizens and Servants._] |
| 79 | + |
| 80 | + MONTAGUE. |
| 81 | + Who set this ancient quarrel new abroach? |
| 82 | + Speak, nephew, were you by when it began? |
| 83 | + |
| 84 | + BENVOLIO. |
| 85 | + Here were the servants of your adversary |
| 86 | + And yours, close fighting ere I did approach. |
| 87 | + I drew to part them, in the instant came |
| 88 | + The fiery Tybalt, with his sword prepar’d, |
| 89 | + Which, as he breath’d defiance to my ears, |
| 90 | + He swung about his head, and cut the winds, |
| 91 | + Who nothing hurt withal, hiss’d him in scorn. |
| 92 | + While we were interchanging thrusts and blows |
| 93 | + Came more and more, and fought on part and part, |
| 94 | + Till the Prince came, who parted either part. |
| 95 | + |
| 96 | + LADY MONTAGUE. |
| 97 | + O where is Romeo, saw you him today? |
| 98 | + Right glad I am he was not at this fray. |
| 99 | + |
| 100 | + BENVOLIO. |
| 101 | + Madam, an hour before the worshipp’d sun |
| 102 | + Peer’d forth the golden window of the east, |
| 103 | + A troubled mind drave me to walk abroad, |
| 104 | + Where underneath the grove of sycamore |
| 105 | + That westward rooteth from this city side, |
| 106 | + So early walking did I see your son. |
| 107 | + Towards him I made, but he was ware of me, |
| 108 | + And stole into the covert of the wood. |
| 109 | + I, measuring his affections by my own, |
| 110 | + Which then most sought where most might not be found, |
| 111 | + Being one too many by my weary self, |
| 112 | + Pursu’d my humour, not pursuing his, |
| 113 | + And gladly shunn’d who gladly fled from me. |
| 114 | + |
| 115 | + MONTAGUE. |
| 116 | + Many a morning hath he there been seen, |
| 117 | + With tears augmenting the fresh morning’s dew, |
| 118 | + Adding to clouds more clouds with his deep sighs; |
| 119 | + But all so soon as the all-cheering sun |
| 120 | + Should in the farthest east begin to draw |
| 121 | + The shady curtains from Aurora’s bed, |
| 122 | + Away from light steals home my heavy son, |
| 123 | + And private in his chamber pens himself, |
| 124 | + Shuts up his windows, locks fair daylight out |
| 125 | + And makes himself an artificial night. |
| 126 | + Black and portentous must this humour prove, |
| 127 | + Unless good counsel may the cause remove. |
| 128 | + |
| 129 | + BENVOLIO. |
| 130 | + My noble uncle, do you know the cause? |
| 131 | + |
| 132 | + MONTAGUE. |
| 133 | + I neither know it nor can learn of him. |
| 134 | + |
| 135 | + BENVOLIO. |
| 136 | + Have you importun’d him by any means? |
| 137 | + |
| 138 | + MONTAGUE. |
| 139 | + Both by myself and many other friends; |
| 140 | + But he, his own affections’ counsellor, |
| 141 | + Is to himself—I will not say how true— |
| 142 | + But to himself so secret and so close, |
| 143 | + So far from sounding and discovery, |
| 144 | + As is the bud bit with an envious worm |
| 145 | + Ere he can spread his sweet leaves to the air, |
| 146 | + Or dedicate his beauty to the sun. |
| 147 | + Could we but learn from whence his sorrows grow, |
| 148 | + We would as willingly give cure as know. |
| 149 | + ` |
| 150 | + |
| 151 | + result := trie.Test(text) |
| 152 | + if result != nil { |
| 153 | + t.Errorf("Expected no profanity but got '%s'", *result) |
| 154 | + } |
| 155 | +} |
0 commit comments