twelve_days_test.go (3831B)
1 package twelve 2 3 import ( 4 "fmt" 5 "strings" 6 "testing" 7 ) 8 9 type testCase struct { 10 input int 11 expected string 12 } 13 14 var testCases = []testCase{ 15 {1, "On the first day of Christmas my true love gave to me: a Partridge in a Pear Tree."}, 16 {2, "On the second day of Christmas my true love gave to me: two Turtle Doves, and a Partridge in a Pear Tree."}, 17 {3, "On the third day of Christmas my true love gave to me: three French Hens, two Turtle Doves, and a Partridge in a Pear Tree."}, 18 {4, "On the fourth day of Christmas my true love gave to me: four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree."}, 19 {5, "On the fifth day of Christmas my true love gave to me: five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree."}, 20 {6, "On the sixth day of Christmas my true love gave to me: six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree."}, 21 {7, "On the seventh day of Christmas my true love gave to me: seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree."}, 22 {8, "On the eighth day of Christmas my true love gave to me: eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree."}, 23 {9, "On the ninth day of Christmas my true love gave to me: nine Ladies Dancing, eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree."}, 24 {10, "On the tenth day of Christmas my true love gave to me: ten Lords-a-Leaping, nine Ladies Dancing, eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree."}, 25 {11, "On the eleventh day of Christmas my true love gave to me: eleven Pipers Piping, ten Lords-a-Leaping, nine Ladies Dancing, eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree."}, 26 {12, "On the twelfth day of Christmas my true love gave to me: twelve Drummers Drumming, eleven Pipers Piping, ten Lords-a-Leaping, nine Ladies Dancing, eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree."}, 27 } 28 29 // diff compares two multi-line strings and returns a helpful comment 30 func diff(got, want string) string { 31 g := strings.Split(got, "\n") 32 w := strings.Split(want, "\n") 33 for i := 0; ; i++ { 34 switch { 35 case i < len(g) && i < len(w): 36 if g[i] == w[i] { 37 continue 38 } 39 return fmt.Sprintf("-- first difference in line %d:\n"+ 40 "-- got : %q\n-- want: %q\n", i+1, g[i], w[i]) 41 case i < len(g): 42 return fmt.Sprintf("-- got %d extra lines after line %d:\n"+ 43 "-- first extra line: %q\n", len(g)-len(w), i, g[i]) 44 case i < len(w): 45 return fmt.Sprintf("-- got %d correct lines, want %d more lines:\n"+ 46 "-- want next: %q\n", i, len(w)-i, w[i]) 47 default: 48 return "no differences found" 49 } 50 } 51 } 52 53 func TestSong(t *testing.T) { 54 var expected = "" 55 for _, test := range testCases { 56 expected += test.expected + "\n" 57 } 58 actual := Song() 59 if expected != actual { 60 t.Fatalf("Song() =\n%s\n want:\n%s\n%s", actual, expected, diff(actual, expected)) 61 } 62 } 63 64 func TestVerse(t *testing.T) { 65 for _, test := range testCases { 66 actual := Verse(test.input) 67 if actual != test.expected { 68 t.Errorf("Twelve Days test [%d], expected [%s], actual [%s]", test.input, test.expected, actual) 69 } 70 } 71 }