package cif import ( "reflect" "testing" "git.fjla.uk/owlboard/go-types/pkg/database" "git.fjla.uk/owlboard/go-types/pkg/upstreamApi" ) func TestParseSpeed(t *testing.T) { testCases := []struct { input string expected int32 }{ {"075", 75}, {"125", 125}, {"40", 40}, {"040", 40}, {"134", 60}, {"179", 80}, {"186", 186}, {"417", 186}, } for _, tc := range testCases { result := parseSpeed(&tc.input) if result != tc.expected { t.Errorf("For speed: %s, expected: %d, but got: %d", tc.input, tc.expected, result) } } nilResult := parseSpeed(nil) if nilResult != 0 { t.Errorf("parseSpeed failed to handle nil pointer, expected %d, but got %d", 0, nilResult) } } func TestIsPublic(t *testing.T) { testCases := []struct { input upstreamApi.CifScheduleLocation expect bool }{ {upstreamApi.CifScheduleLocation{ LocationType: "LO", RecordIdentity: "", TiplocCode: "BATHSPA", TiplocInstance: "0", Arrival: "0422H", Departure: "0445", PublicDeparture: "0445", Pass: "", Path: "UFR", Platform: "1", Line: "DFR", EngineeringAllowance: "", PathingAllowance: "", PerformanceAllowance: "", }, true}, {upstreamApi.CifScheduleLocation{ LocationType: "LO", RecordIdentity: "", TiplocCode: "BATHSPA", TiplocInstance: "0", Arrival: "0422H", Departure: "0445", PublicDeparture: "", Pass: "", Path: "UFR", Platform: "1", Line: "DFR", EngineeringAllowance: "", PathingAllowance: "", PerformanceAllowance: "", }, false}, } for num, tc := range testCases { result := isPublic(&tc.input) if result != tc.expect { t.Errorf("For testCase %d, expected %t, but got %t", num, tc.expect, result) } } } func TestHasFirstClass(t *testing.T) { testCases := []struct { input string headcode string expect bool }{ {"", "1A00", true}, {"B", "2A05", true}, {"S", "1C99", false}, {"", "3C23", false}, {"", "5Q21", false}, {"", "5D32", false}, {"", "9O12", true}, {"B", "9D32", true}, {"", "7R43", false}, {"B", "6Y77", false}, {"", "8P98", false}, {"S", "4O89", false}, {"", "4E43", false}, } for _, tc := range testCases { result := hasFirstClass(&tc.input, &tc.headcode) if result != tc.expect { t.Errorf("For %s & headcode %s, expected %t, but got %t", tc.input, tc.headcode, tc.expect, result) } } nilResult := hasFirstClass(nil, nil) if nilResult { t.Errorf("hasFirstClass failed to handle nil pointer, expected %t, got %t", false, nilResult) } } func TestHasCatering(t *testing.T) { testCases := []struct { input string expect bool }{ {"", false}, {"CF", true}, {"HT", true}, {"MR", true}, {"RP", true}, {"T", true}, {"F", true}, } for _, tc := range testCases { result := hasCatering(&tc.input) if result != tc.expect { t.Errorf("For %s, expected %t, but got %t", tc.input, tc.expect, result) } } nilResult := hasCatering(nil) if nilResult { t.Errorf("hasCatering failed to handle nil pointer, expected %t, but got %t", false, nilResult) } } func TestHasSleeper(t *testing.T) { testCases := []struct { input string expect bool }{ {"B", true}, {"F", true}, {"S", true}, {"", false}, } for _, tc := range testCases { result := hasSleeper(&tc.input) if result != tc.expect { t.Errorf("For %s, expected %t, but got %t", tc.input, tc.expect, result) } } nilResult := hasSleeper(nil) if nilResult { t.Errorf("hasSleeper failed to handle nil pointer, expected %t, but got %t", false, nilResult) } } func TestParseStops(t *testing.T) { testCases := []struct { input []upstreamApi.CifScheduleLocation expected []database.Stop }{ { input: []upstreamApi.CifScheduleLocation{ { LocationType: "LO", RecordIdentity: "Yes", TiplocCode: "BATHSPA", TiplocInstance: "0", Arrival: "0445", Departure: "0449", PublicDeparture: "0449", Pass: "", Platform: "1", Line: "DM", Path: "DM", EngineeringAllowance: "", PathingAllowance: "", PerformanceAllowance: "", }}, expected: []database.Stop{ { PublicDeparture: "0449", WttDeparture: "0449", PublicArrival: "", WttArrival: "0445", IsPublic: true, Tiploc: "BATHSPA", Pass: "", Platform: "1", ArrLine: "DM", DepLine: "DM", }, }, }, } for _, tc := range testCases { result := parseStops(&tc.input) if !reflect.DeepEqual(result, tc.expected) { t.Errorf("Test case failed. Input: %v, Expected: %v, Got: %v", tc.input, tc.expected, result) } } }