From 65d800fef6ac2d5ca4b02dab11368c873e37be46 Mon Sep 17 00:00:00 2001 From: Sean DeNigris Date: Mon, 9 Sep 2024 21:49:15 -0400 Subject: [PATCH] [Enh]: Number Reading - Thousands Separators --- .../MANumberDescription.class.st | 22 +++++++++++++++++++ source/Magritte-Model/MAStringReader.class.st | 7 ++++-- .../MANumberDescriptionTest.class.st | 21 +++++++++++++++++- 3 files changed, 47 insertions(+), 3 deletions(-) diff --git a/source/Magritte-Model/MANumberDescription.class.st b/source/Magritte-Model/MANumberDescription.class.st index fa592f75..0cd75ca2 100644 --- a/source/Magritte-Model/MANumberDescription.class.st +++ b/source/Magritte-Model/MANumberDescription.class.st @@ -12,6 +12,12 @@ MANumberDescription class >> defaultKind [ ^ Number ] +{ #category : #'accessing-defaults' } +MANumberDescription class >> defaultThousandsSeparator [ + + ^ $, +] + { #category : #testing } MANumberDescription class >> isAbstract [ ^ false @@ -41,3 +47,19 @@ MANumberDescription >> beNegative [ MANumberDescription >> bePositive [ self addCondition: (MACondition selector: #positive) labelled: 'No positive number was entered' ] + +{ #category : #accessing } +MANumberDescription >> thousandsSeparator [ + + ^ self + propertyAt: #thousandsSeparator + ifAbsent: [ self class defaultThousandsSeparator ] +] + +{ #category : #accessing } +MANumberDescription >> thousandsSeparator: aCharacter [ + + ^ self + propertyAt: #thousandsSeparator + putRemovingNil: aCharacter +] diff --git a/source/Magritte-Model/MAStringReader.class.st b/source/Magritte-Model/MAStringReader.class.st index e3792ca9..07f6e443 100644 --- a/source/Magritte-Model/MAStringReader.class.st +++ b/source/Magritte-Model/MAStringReader.class.st @@ -114,9 +114,12 @@ MAStringReader >> visitMultipleOptionDescription: aDescription [ { #category : #'visiting-description' } MAStringReader >> visitNumberDescription: aDescription [ - | isContentsValid | - isContentsValid := NumberParser isValidNumber: self contents. + | isContentsValid cleanedContents | + cleanedContents := self contents reject: [ :e | e = aDescription thousandsSeparator ]. + isContentsValid := NumberParser isValidNumber: cleanedContents. isContentsValid ifFalse: [ MAReadError signal ]. + + self stream: cleanedContents readStream. super visitNumberDescription: aDescription ] diff --git a/source/Magritte-Tests-Model/MANumberDescriptionTest.class.st b/source/Magritte-Tests-Model/MANumberDescriptionTest.class.st index d76c1ff2..0098c43a 100644 --- a/source/Magritte-Tests-Model/MANumberDescriptionTest.class.st +++ b/source/Magritte-Tests-Model/MANumberDescriptionTest.class.st @@ -1,7 +1,7 @@ Class { #name : #MANumberDescriptionTest, #superclass : #MAMagnitudeDescriptionTest, - #category : 'Magritte-Tests-Model-Description' + #category : #'Magritte-Tests-Model-Description' } { #category : #testing } @@ -85,6 +85,25 @@ MANumberDescriptionTest >> testFromString [ = -20 description: 'Negative numbers should be accepted' ] +{ #category : #private } +MANumberDescriptionTest >> testThousandsSeparatorCustom [ + + self + assert: (self description + thousandsSeparator: $.; + fromString: '20.000') + equals: 20000 "By default, commas should be ignored" +] + +{ #category : #private } +MANumberDescriptionTest >> testThousandsSeparatorDefault [ + + "self assert: (self description fromString: '') isNil description: 'Empty string should be parsed to nil'." + self + assert: (self description fromString: '20,000') + equals: 20000 "By default, commas should be ignored" +] + { #category : #tests } MANumberDescriptionTest >> testValidateConditions [ ]