|
| 1 | +package fname |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func TestNewGenerator(t *testing.T) { |
| 9 | + t.Log("Given the need to test the NewGenerator function") |
| 10 | + { |
| 11 | + t.Log("\tWhen creating a new Generator with default values") |
| 12 | + { |
| 13 | + g := NewGenerator() |
| 14 | + if g == nil { |
| 15 | + t.Fatal("\t\tShould be able to create a Generator instance.") |
| 16 | + } |
| 17 | + t.Log("\t\tShould be able to create a Generator instance.") |
| 18 | + |
| 19 | + if g.dict == nil { |
| 20 | + t.Fatal("\t\tShould be able to create a Dictionary instance.") |
| 21 | + } |
| 22 | + t.Log("\t\tShould be able to create a Dictionary instance.") |
| 23 | + } |
| 24 | + |
| 25 | + t.Log("\tWhen creating a new Generator with custom values") |
| 26 | + { |
| 27 | + g := NewGenerator(WithDelimiter("_"), WithSize(3), WithSeed(12345)) |
| 28 | + if g == nil { |
| 29 | + t.Fatal("\t\tShould be able to create a Generator instance.") |
| 30 | + } |
| 31 | + t.Log("\t\tShould be able to create a Generator instance.") |
| 32 | + |
| 33 | + if g.size != 3 { |
| 34 | + t.Fatal("\t\tShould be able to set the size of the phrase.") |
| 35 | + } |
| 36 | + t.Log("\t\tShould be able to set the size of the phrase.") |
| 37 | + |
| 38 | + if g.delimiter != "_" { |
| 39 | + t.Fatal("\t\tShould be able to set the delimiter of the phrase.") |
| 40 | + } |
| 41 | + t.Log("\t\tShould be able to set the delimiter of the phrase.") |
| 42 | + |
| 43 | + if g.seed != 12345 { |
| 44 | + t.Fatal("\t\tShould be able to set the seed of the phrase.") |
| 45 | + } |
| 46 | + t.Log("\t\tShould be able to set the seed of the phrase.") |
| 47 | + } |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func TestGenerate(t *testing.T) { |
| 52 | + t.Log("Given the need to test the Generate function") |
| 53 | + { |
| 54 | + t.Log("\tWhen generating a phrase") |
| 55 | + { |
| 56 | + g := NewGenerator() |
| 57 | + phrase, err := g.Generate() |
| 58 | + if err != nil { |
| 59 | + t.Fatal("\t\tShould be able to generate a phrase without error.") |
| 60 | + } |
| 61 | + t.Log("\t\tShould be able to generate a phrase without error.") |
| 62 | + |
| 63 | + if len(phrase) == 0 { |
| 64 | + t.Fatal("\t\tShould be able to generate a phrase of non-zero length.") |
| 65 | + } |
| 66 | + t.Log("\t\tShould be able to generate a phrase of non-zero length.") |
| 67 | + |
| 68 | + parts := strings.Split(phrase, "-") |
| 69 | + if len(parts) != 2 { |
| 70 | + t.Fatal("\t\tShould be able to generate a phrase with 2 parts.") |
| 71 | + } |
| 72 | + t.Log("\t\tShould be able to generate a phrase with 2 parts.") |
| 73 | + } |
| 74 | + |
| 75 | + t.Log("\tWhen generating a phrase with a custom delimiter") |
| 76 | + { |
| 77 | + g := NewGenerator(WithDelimiter("_")) |
| 78 | + phrase, err := g.Generate() |
| 79 | + if err != nil { |
| 80 | + t.Fatal("\t\tShould be able to generate a phrase without error.") |
| 81 | + } |
| 82 | + t.Log("\t\tShould be able to generate a phrase without error.") |
| 83 | + |
| 84 | + if len(phrase) == 0 { |
| 85 | + t.Fatal("\t\tShould be able to generate a phrase of non-zero length.") |
| 86 | + } |
| 87 | + t.Log("\t\tShould be able to generate a phrase of non-zero length.") |
| 88 | + |
| 89 | + parts := strings.Split(phrase, "_") |
| 90 | + if len(parts) != 2 { |
| 91 | + t.Fatal("\t\tShould be able to generate a phrase with 2 parts.") |
| 92 | + } |
| 93 | + t.Log("\t\tShould be able to generate a phrase with 2 parts.") |
| 94 | + |
| 95 | + if !strings.Contains(phrase, "_") { |
| 96 | + t.Fatal("\t\tShould be able to generate a phrase with the custom delimiter.") |
| 97 | + } |
| 98 | + t.Log("\t\tShould be able to generate a phrase with the custom delimiter.") |
| 99 | + } |
| 100 | + |
| 101 | + t.Log("\tWhen generating a phrase with a custom size") |
| 102 | + { |
| 103 | + g3 := NewGenerator(WithSize(3)) |
| 104 | + phrase, err := g3.Generate() |
| 105 | + if err != nil { |
| 106 | + t.Fatal("\t\tShould be able to generate a phrase without error.") |
| 107 | + } |
| 108 | + t.Log("\t\tShould be able to generate a phrase without error.") |
| 109 | + |
| 110 | + if len(phrase) == 0 { |
| 111 | + t.Fatal("\t\tShould be able to generate a phrase of non-zero length.") |
| 112 | + } |
| 113 | + t.Log("\t\tShould be able to generate a phrase of non-zero length.") |
| 114 | + |
| 115 | + parts := strings.Split(phrase, "-") |
| 116 | + if len(parts) != 3 { |
| 117 | + t.Fatal("\t\tShould be able to generate a phrase with 3 parts.") |
| 118 | + } |
| 119 | + t.Log("\t\tShould be able to generate a phrase with 3 parts.") |
| 120 | + |
| 121 | + g4 := NewGenerator(WithSize(4)) |
| 122 | + phrase, err = g4.Generate() |
| 123 | + if err != nil { |
| 124 | + t.Fatal("\t\tShould be able to generate a phrase without error.") |
| 125 | + } |
| 126 | + t.Log("\t\tShould be able to generate a phrase without error.") |
| 127 | + |
| 128 | + if len(phrase) == 0 { |
| 129 | + t.Fatal("\t\tShould be able to generate a phrase of non-zero length.") |
| 130 | + } |
| 131 | + t.Log("\t\tShould be able to generate a phrase of non-zero length.") |
| 132 | + |
| 133 | + parts = strings.Split(phrase, "-") |
| 134 | + if len(parts) != 4 { |
| 135 | + t.Fatal("\t\tShould be able to generate a phrase with 4 parts.") |
| 136 | + } |
| 137 | + t.Log("\t\tShould be able to generate a phrase with 4 parts.") |
| 138 | + } |
| 139 | + |
| 140 | + t.Log("\tWhen generating a phrase with a custom seed") |
| 141 | + { |
| 142 | + g1 := NewGenerator(WithSeed(12345)) |
| 143 | + phrase1, err := g1.Generate() |
| 144 | + if err != nil { |
| 145 | + t.Fatal("\t\tShould be able to generate a phrase without error.") |
| 146 | + } |
| 147 | + t.Log("\t\tShould be able to generate a phrase without error.") |
| 148 | + |
| 149 | + g2 := NewGenerator(WithSeed(12345)) |
| 150 | + phrase2, err := g2.Generate() |
| 151 | + if err != nil { |
| 152 | + t.Fatal("\t\tShould be able to generate a phrase without error.") |
| 153 | + } |
| 154 | + t.Log("\t\tShould be able to generate a phrase without error.") |
| 155 | + |
| 156 | + if phrase1 != phrase2 { |
| 157 | + t.Fatal("\t\tShould be able to generate the same phrase with the same seed.") |
| 158 | + } |
| 159 | + t.Log("\t\tShould be able to generate the same phrase with the same seed.") |
| 160 | + } |
| 161 | + |
| 162 | + t.Log("\tWhen generating a phrase with an invalid size") |
| 163 | + { |
| 164 | + g := NewGenerator(WithSize(1)) |
| 165 | + _, err := g.Generate() |
| 166 | + if err == nil { |
| 167 | + t.Fatal("\t\tShould not be able to generate a phrase with an invalid size.") |
| 168 | + } |
| 169 | + t.Log("\t\tShould not be able to generate a phrase with an invalid size.") |
| 170 | + } |
| 171 | + } |
| 172 | +} |
0 commit comments