Skip to content

Commit 870e4ee

Browse files
authored
Merge pull request #43 from icweaver/undef
fix(card): Don't write a units field for undefined values
2 parents 8b68009 + 2c31e5b commit 870e4ee

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

src/card.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,8 @@ end
460460
function formatcard(::Type{Value{V}}, value::V, comment::C; kwds...) where
461461
{V <: ValueType, C <: AbstractString}
462462

463-
units = unit(value) === NoUnits ? missing : string(unit(value))
463+
# An undefined value has no units: `unit(missing)` is `missing`, not `NoUnits`.
464+
units = ismissing(value) || unit(value) === NoUnits ? missing : string(unit(value))
464465
# Create appropriate CardFormat type
465466
format = CardFormat(kwds[:fixed])
466467
fmt = string(ustrip(value))

test/card_tests.jl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,28 @@
202202
# create Value Card and get its type with boolean value
203203
@test typeof(Card("BOOL", true)) <: Card{Value{Bool}}
204204

205+
# create Value card with undefined value
206+
@test isequal(showfields(Card("UNDEF", missing)),
207+
("UNDEF", missing, "",
208+
"UNDEF = "))
209+
210+
# create Value card with undefined value and comment. An undefined value has no
211+
# units, so no units field is written.
212+
@test isequal(showfields(Card("UNDEF", missing, "a comment")),
213+
("UNDEF", missing, "a comment",
214+
"UNDEF = / a comment "))
215+
216+
# an undefined value card round trips through its image
217+
@test isequal(showfields(parse(Card,
218+
"UNDEF = / a comment ")),
219+
("UNDEF", missing, "a comment",
220+
"UNDEF = / a comment "))
221+
222+
# a units string in the comment of an undefined value card is preserved
223+
@test isequal(showfields(Card("UNDEF", missing, "[m] a comment")),
224+
("UNDEF", missing, "[m] a comment",
225+
"UNDEF = / [m] a comment "))
226+
205227
# create Value card with boolean value, fixed-format
206228
@test isequal(showfields(Card("BOOL", false)),
207229
("BOOL", false, "",

0 commit comments

Comments
 (0)