summaryrefslogtreecommitdiff
path: root/internal/rpn/parse_category_test.go
blob: 8f67b9f1f2a4c79afc5072d262d28a264bf371fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// SPDX-License-Identifier: MIT
// Copyright (c) 2026 Paul Buetow

package rpn

import "testing"

// TestParseCategoryKnown tests that known category names resolve correctly.
func TestParseCategoryKnown(t *testing.T) {
	tests := []struct {
		name string
		want Category
	}{
		{"Universal", Universal},
		{"DataRate", DataRate},
		{"DataSize", DataSize},
		{"Time", Time},
		{"Weight", Weight},
		{"Speed", Speed},
		{"Distance", Distance},
		{"Custom", Custom},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got, ok := parseCategory(tt.name)
			if !ok {
				t.Errorf("parseCategory(%q) = !ok, want ok", tt.name)
			}
			if got != tt.want {
				t.Errorf("parseCategory(%q) = %v, want %v", tt.name, got, tt.want)
			}
		})
	}
}

// TestParseCategoryUnknown does not return Universal.
// If parseCategory returned Category(0) on failure, callers ignoring the
// bool return would silently get Universal — this test prevents that regression.
func TestParseCategoryUnknown(t *testing.T) {
	got, ok := parseCategory("DoesNotExist")
	if ok {
		t.Fatal("parseCategory(\"DoesNotExist\") = ok, want !ok")
	}
	if got == Universal {
		t.Errorf("parseCategory(\"DoesNotExist\") returned Universal (Category(0)); "+
			"use a sentinel like Category(-1) so callers can't mistake it for a valid category")
	}
	if got != invalidCategory {
		t.Errorf("parseCategory(\"DoesNotExist\") = %v, want %v (invalidCategory)", got, invalidCategory)
	}
}

// TestParseCategoryEmpty tests that an empty string is not treated as a valid category.
func TestParseCategoryEmpty(t *testing.T) {
	got, ok := parseCategory("")
	if ok {
		t.Fatal("parseCategory(\"\") = ok, want !ok")
	}
	if got == Universal {
		t.Error("parseCategory(\"\") must not return Universal")
	}
}

// TestInvalidCategorySentinel verifies the sentinel is not a valid category.
func TestInvalidCategorySentinel(t *testing.T) {
	if invalidCategory >= 0 && invalidCategory < _sentinel {
		t.Errorf("invalidCategory (%d) overlaps with valid range [0, %d)", invalidCategory, _sentinel)
	}
	if invalidCategory == Universal {
		t.Error("invalidCategory must never equal Universal")
	}
}