-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunit_conversion_test.go
More file actions
154 lines (132 loc) · 4.01 KB
/
unit_conversion_test.go
File metadata and controls
154 lines (132 loc) · 4.01 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package cooklang
import (
"testing"
)
func TestIngredientUnitConversion(t *testing.T) {
// Test parsing an ingredient with units
recipe, err := ParseString("Add @flour{500%g} to the bowl")
if err != nil {
t.Fatalf("Failed to parse recipe: %v", err)
}
// Get the first ingredient
var flour *Ingredient
currentStep := recipe.FirstStep
currentComponent := currentStep.FirstComponent
for currentComponent != nil {
if ingredient, ok := currentComponent.(*Ingredient); ok && ingredient.Name == "flour" {
flour = ingredient
break
}
currentComponent = currentComponent.GetNext()
}
if flour == nil {
t.Fatal("Flour ingredient not found")
}
// Test basic properties
if flour.Name != "flour" {
t.Errorf("Expected ingredient name 'flour', got '%s'", flour.Name)
}
if flour.Quantity != 500 {
t.Errorf("Expected quantity 500, got %f", flour.Quantity)
}
if flour.Unit != "g" {
t.Errorf("Expected unit 'g', got '%s'", flour.Unit)
}
if flour.TypedUnit == nil {
t.Error("TypedUnit should not be nil")
}
// Test unit type detection
unitType := flour.GetUnitType()
if unitType != "mass" {
t.Logf("Note: Unit type for 'g' is '%s', expected 'mass' (this might be expected behavior)", unitType)
}
// Test conversion capabilities
t.Run("ConvertToKilograms", func(t *testing.T) {
converted, err := flour.ConvertTo("kg")
if err != nil {
// This might fail if go-units doesn't have conversion between g and kg
t.Logf("Conversion from g to kg failed (this might be expected): %v", err)
return
}
if converted.Unit != "kg" {
t.Errorf("Expected converted unit 'kg', got '%s'", converted.Unit)
}
// Should be 0.5 kg
expectedQuantity := float32(0.5)
if abs(converted.Quantity-expectedQuantity) > 0.001 {
t.Errorf("Expected converted quantity %f, got %f", expectedQuantity, converted.Quantity)
}
})
t.Run("CanConvertTo", func(t *testing.T) {
// Test if can convert to kg
canConvert := flour.CanConvertTo("kg")
t.Logf("Can convert from g to kg: %t", canConvert)
// Test if can convert to incompatible unit
canConvertToIncompatible := flour.CanConvertTo("liter")
t.Logf("Can convert from g to liter: %t", canConvertToIncompatible)
})
}
func TestIngredientWithoutUnits(t *testing.T) {
// Test parsing an ingredient without units
recipe, err := ParseString("Add @salt to taste")
if err != nil {
t.Fatalf("Failed to parse recipe: %v", err)
}
// Get the first ingredient
var salt *Ingredient
currentStep := recipe.FirstStep
currentComponent := currentStep.FirstComponent
for currentComponent != nil {
if ingredient, ok := currentComponent.(*Ingredient); ok && ingredient.Name == "salt" {
salt = ingredient
break
}
currentComponent = currentComponent.GetNext()
}
if salt == nil {
t.Fatal("Salt ingredient not found")
}
// Should have no typed unit for unitless ingredients
if salt.TypedUnit != nil {
t.Error("TypedUnit should be nil for ingredients without units")
}
unitType := salt.GetUnitType()
if unitType != "" {
t.Errorf("Expected empty unit type for unitless ingredient, got '%s'", unitType)
}
}
func TestIngredientWithCustomUnits(t *testing.T) {
// Test parsing an ingredient with custom/non-standard units
recipe, err := ParseString("Add @eggs{3%pieces}")
if err != nil {
t.Fatalf("Failed to parse recipe: %v", err)
}
// Get the first ingredient
var eggs *Ingredient
currentStep := recipe.FirstStep
currentComponent := currentStep.FirstComponent
for currentComponent != nil {
if ingredient, ok := currentComponent.(*Ingredient); ok && ingredient.Name == "eggs" {
eggs = ingredient
break
}
currentComponent = currentComponent.GetNext()
}
if eggs == nil {
t.Fatal("Eggs ingredient not found")
}
// Should have a typed unit even for custom units
if eggs.TypedUnit == nil {
t.Error("TypedUnit should not be nil even for custom units")
}
if eggs.Unit != "pieces" {
t.Errorf("Expected unit 'pieces', got '%s'", eggs.Unit)
}
}
// Helper function for floating point comparison
func abs(x float32) float32 {
if x < 0 {
return -x
}
return x
}