Source

newspeak / PatternMatchingTests.ns3

The default branch has multiple heads

Newspeak3
'Pattern-Matching'
class PatternMatchingTests usingPlatform: platform testFramework: minitest = NewspeakObject (
"Tests for Newspeaks pattern matching facilities."|
	private TestContext = minitest TestContext.
	private MessageNotUnderstood = platform MessageNotUnderstood.
|)
(
class LexicalScopingTests = TestContext (
"If a pattern defines a pattern variable (e.g., ?x) and one uses the application combinator on such a pattern, then the pattern variable should only be accessible in closures (that are parameters to the application combinator) that are defined in the same context as the pattern that defines the pattern variable.

This class contains tests that ensure that this is true."|
	patternSlot
|)
(
class TestSubject = (|
|)
('as yet unclassified'
match: p = (
	^ p testSubject: self
))'as yet unclassified'
pattern ^ <Pattern> = (
	^ <testSubject: ?x>.
)
setPattern = (
	patternSlot: pattern.
)
testLexicalScopeAccessClosureThroughMethod = (
|	a |
	setPattern.
	[ a:: TestSubject new 
			case: <testSubject: ?x> => variableXAccessor 
			otherwise: [ false ]]
	on: MessageNotUnderstood do: [ :mnu | a:: mnu ].
	assert: a message selector = 'x'.
)
testLexicalScopeAccessPatternThroughGetter = (
|	a |
	setPattern.
	[ a:: TestSubject new case: patternSlot => [ x ] otherwise: [ false ]]
	on: MessageNotUnderstood do: [ :mnu | a:: mnu ].
	assert: a message selector = 'x'.
)
testLexicalScopeAccessPatternThroughMethod = (
|	a |
	setPattern.
	[ a:: TestSubject new case: pattern => [ x ] otherwise: [ false ]]
	on: MessageNotUnderstood do: [ :mnu | a:: mnu ].
	unsetPattern.
	assert: a message selector = 'x'.
)
testTestSubject = (
|	ts = TestSubject new. 
	a |
	a:: ts case: <testSubject: ?x> => [ x ] otherwise: [ false ].
	assert: a == ts.
)
unsetPattern = (
	patternSlot: nil.
)
variableXAccessor ^ <[]> = (
	^ [ x ]
)) : ('as yet unclassified'
TEST_CONTEXT = (
	
))
class SimplificationTests = TestContext (|
|)
(
class Num of: n = (|
	val = n.
|)
('as yet unclassified'
match: p = (
	^ p num: val
))
class Product of: l and: r = (|
	lhs =l.
	rhs = r.
|)
('as yet unclassified'
match: p = (
	^ p multiply: lhs with: rhs
))'as yet unclassified'
simplify: expr = (
	^ expr
		case: <multiply: ?x with: <num: 1>> => [ x ]
		otherwise: [ expr ]
)
testSimpleSimplify = (
|	three = Num of: 3. 
	result = simplify: three. |
	assert: result = three.
)
testSimplificationForMultiplicationIdentity = (
|	lhs = Num of: 3.
	rhs = Num of: 1.
	product = Product of: lhs and: rhs.
	result = simplify: product. |
	assert: result = lhs.
)) : ('as yet unclassified'
TEST_CONTEXT = (
	
)))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.