package main import "testing" func TestParseArgsCPU(t *testing.T) { opts, err := parseArgs([]string{"--cpu", "Ryzen 3600x"}) if err != nil { t.Fatalf("parseArgs returned error: %v", err) } if opts.kind != kindCPU || len(opts.queries) != 1 || opts.queries[0] != "Ryzen 3600x" { t.Fatalf("unexpected options: %+v", opts) } } func TestParseArgsGPU(t *testing.T) { opts, err := parseArgs([]string{"--gpu", "Radeon RX 6650 XT"}) if err != nil { t.Fatalf("parseArgs returned error: %v", err) } if opts.kind != kindGPU || len(opts.queries) != 1 || opts.queries[0] != "Radeon RX 6650 XT" { t.Fatalf("unexpected options: %+v", opts) } } func TestParseArgsCPUCompare(t *testing.T) { opts, err := parseArgs([]string{"--cpu", "Ryzen 3600x", "Core i7-10700"}) if err != nil { t.Fatalf("parseArgs returned error: %v", err) } if opts.kind != kindCPU || len(opts.queries) != 2 || opts.queries[0] != "Ryzen 3600x" || opts.queries[1] != "Core i7-10700" { t.Fatalf("unexpected options: %+v", opts) } } func TestParseArgsCPUMany(t *testing.T) { opts, err := parseArgs([]string{"--cpu", "Ryzen 3600x", "Core i7-10700", "Core i5-12400", "Ryzen 5800X3D"}) if err != nil { t.Fatalf("parseArgs returned error: %v", err) } if opts.kind != kindCPU || len(opts.queries) != 4 { t.Fatalf("unexpected options: %+v", opts) } want := []string{"Ryzen 3600x", "Core i7-10700", "Core i5-12400", "Ryzen 5800X3D"} for i := range want { if opts.queries[i] != want[i] { t.Fatalf("query %d = %q, want %q; all options: %+v", i, opts.queries[i], want[i], opts) } } } func TestComparisonRowsMany(t *testing.T) { results := []result{ {Kind: kindCPU, Name: "A", Stats: []stat{{Name: "PassMark Score", Value: "10"}, {Name: "Price", Value: "$300"}}}, {Kind: kindCPU, Name: "B", Stats: []stat{{Name: "PassMark Score", Value: "20"}, {Name: "Price", Value: "$200"}}}, {Kind: kindCPU, Name: "C", Stats: []stat{{Name: "PassMark Score", Value: "15"}, {Name: "Price", Value: "$250"}}}, } rows := comparisonRows(results) if len(rows) < 2 { t.Fatalf("expected comparison rows, got %+v", rows) } score := findRow(rows, "PassMark Score") if len(score) != 5 { t.Fatalf("score row has %d cells, want 5: %+v", len(score), score) } if score[4] != "B" { t.Fatalf("score winner = %q, want B; row: %+v", score[4], score) } price := findRow(rows, "Price") if price[4] != "B" { t.Fatalf("price winner = %q, want B; row: %+v", price[4], price) } } func TestCompareStatPriceLowerIsBetter(t *testing.T) { cmp, ok := compareStat("Price", "$97.71", "$299.99") if !ok { t.Fatal("expected price to compare") } if cmp <= 0 { t.Fatalf("cmp = %d, want left price to win", cmp) } } func TestCompareStatPassMarkHigherIsBetter(t *testing.T) { cmp, ok := compareStat("PassMark Score", "18,123", "17,111") if !ok { t.Fatal("expected score to compare") } if cmp <= 0 { t.Fatalf("cmp = %d, want left score to win", cmp) } } func TestCompareStatReleaseDateNewerIsBetter(t *testing.T) { cmp, ok := compareStat("CPU First Seen on Charts", "Jan 2024", "January 2021") if !ok { t.Fatal("expected release date to compare") } if cmp <= 0 { t.Fatalf("cmp = %d, want newer date to win", cmp) } } func TestCompareStatQuarterDateNewerIsBetter(t *testing.T) { cmp, ok := compareStat("CPU First Seen on Charts", "Q3 2019", "Q2 2018") if !ok { t.Fatal("expected quarter date to compare") } if cmp <= 0 { t.Fatalf("cmp = %d, want newer quarter to win", cmp) } } func TestCompareStatSocketIsNotNumeric(t *testing.T) { if _, ok := compareStat("Socket", "AM4", "FCLGA1200"); ok { t.Fatal("socket values should not be treated as numeric comparisons") } } func TestCanonicalDetailURLCPU(t *testing.T) { got := canonicalDetailURL(kindCPU, "https://www.cpubenchmark.net/cpu_lookup.php?cpu=Intel+Core+i7&id=3747") want := "https://www.cpubenchmark.net/cpu.php?cpu=Intel+Core+i7&id=3747" if got != want { t.Fatalf("canonicalDetailURL = %q, want %q", got, want) } } func TestPassMarkSelectedStatsCPU(t *testing.T) { html := `
  • AMD Ryzen 5 3600X185.518,123$97.71
  • ` stats := passmarkSelectedStats(html) want := map[string]string{ "Matched Product": "AMD Ryzen 5 3600X", "PassMark Score": "18,123", "Value": "185.5", "Price": "$97.71", "Rank": "1103", "Samples": "8702", } assertStats(t, stats, want) } func TestPassMarkSelectedStatsGPU(t *testing.T) { html := `
  • Radeon RX 6650 XT57.017111299.99*
  • ` stats := passmarkSelectedStats(html) want := map[string]string{ "Matched Product": "Radeon RX 6650 XT", "PassMark Score": "17111", "Value": "57.0", "Price": "299.99*", "Rank": "124", "Samples": "5093", } assertStats(t, stats, want) } func TestBestCandidateSplitsGPUModelNumbers(t *testing.T) { best, ok := bestCandidate("Radeon RX6650xt", []candidate{ {Name: "256MB DDR Radeon 9800 XT", URL: "old"}, {Name: "Radeon RX 6650 XT", URL: "new"}, }) if !ok { t.Fatal("expected a match") } if best.URL != "new" { t.Fatalf("matched %q, want Radeon RX 6650 XT", best.Name) } } func assertStats(t *testing.T, stats []stat, want map[string]string) { t.Helper() got := make(map[string]string) for _, s := range stats { got[s.Name] = s.Value } for k, v := range want { if got[k] != v { t.Fatalf("stat %q = %q, want %q; all stats: %+v", k, got[k], v, stats) } } } func findRow(rows [][]string, name string) []string { for _, row := range rows { if row[0] == name { return row } } return nil }