|
| 1 | +package rot13 |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | +) |
| 6 | + |
| 7 | +var rot13TestData = []struct { |
| 8 | + description string |
| 9 | + input string |
| 10 | + expected string |
| 11 | +}{ |
| 12 | + { |
| 13 | + "Basic rotation with letter 'a' gives 'n", |
| 14 | + "a", |
| 15 | + "n", |
| 16 | + }, |
| 17 | + { |
| 18 | + "Rotation with wrapping around alphabet on letter 'z' gives 'm'", |
| 19 | + "z", |
| 20 | + "m", |
| 21 | + }, |
| 22 | + { |
| 23 | + "Rotation on 'hello world'", |
| 24 | + "hello world", |
| 25 | + "uryyb jbeyq", |
| 26 | + }, |
| 27 | + { |
| 28 | + "Rotation on the rotation of 'hello world' gives 'hello world' back", |
| 29 | + "uryyb jbeyq", |
| 30 | + "hello world", |
| 31 | + }, |
| 32 | + { |
| 33 | + "Full sentence rotation", |
| 34 | + "the quick brown fox jumps over the lazy dog.", |
| 35 | + "gur dhvpx oebja sbk whzcf bire gur ynml qbt.", |
| 36 | + }, |
| 37 | + { |
| 38 | + "Sentence from Rot13.go main function", |
| 39 | + "we'll just make him an offer he can't refuse... tell me you get the pop culture reference", |
| 40 | + "jr'yy whfg znxr uvz na bssre ur pna'g ershfr... gryy zr lbh trg gur cbc phygher ersrerapr", |
| 41 | + }, |
| 42 | +} |
| 43 | + |
| 44 | +func TestRot13Encrypt(t *testing.T) { |
| 45 | + for _, test := range rot13TestData { |
| 46 | + t.Run(test.description, func(t *testing.T) { |
| 47 | + input := test.input |
| 48 | + expected := test.expected |
| 49 | + assertRot13Output(t, input, expected) |
| 50 | + }) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func TestRot13Decrypt(t *testing.T) { |
| 55 | + for _, test := range rot13TestData { |
| 56 | + t.Run(test.description, func(t *testing.T) { |
| 57 | + input := test.expected |
| 58 | + expected := test.input |
| 59 | + assertRot13Output(t, input, expected) |
| 60 | + }) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +func assertRot13Output(t *testing.T, input, expected string) { |
| 65 | + actual := rot13(input) |
| 66 | + if actual != expected { |
| 67 | + t.Fatalf("With input string '%s' was expecting '%s' but actual was '%s'", |
| 68 | + input, expected, actual) |
| 69 | + } |
| 70 | +} |
0 commit comments