Skip to content

Round-trip conformance

The contract: AsciiChem.parse(s).to_text == s for any canonical s. The Text formatter is the canonicaliser — equivalent inputs map to the same canonical output, and canonical inputs round-trip exactly.

ConstructCanonical form
SubscriptExplicit _: H_2
ChargeNumber-then-sign: Ca^2+
CoefficientLeading digits: 2H_2O
IsotopePrefix ^: ^14C
Oxidation stateParens around Roman: Fe^(III)
GroupBrackets preserved
Reaction arrow->, <-, <=>, <->
InputOutputReason
Ca^+2Ca^2+IUPAC number-then-sign

Everything else. The formatter does not reorder atoms, choose canonical bracket kinds, or rewrite your input chemistry. It only normalises the spellings of individual markers.

The gem ships with spec/conformance/round_trip_spec.rb containing canonical inputs that must round-trip exactly. Run it with:

Terminal window
bundle exec rspec spec/conformance/

Adding a case is the canonical way to lock in a syntax decision: if the case round-trips, the parser and Text formatter agree.