Christoph Thiede uploaded a new version of SUnit to project The Inbox:
http://source.squeak.org/inbox/SUnit-ct.126.mcz ==================== Summary ==================== Name: SUnit-ct.126 Author: ct Time: 9 April 2020, 2:06:17.039949 pm UUID: fa174d9c-ba57-4549-bbce-7945f79eada2 Ancestors: SUnit-mt.121 Refactors and completes equality assertions - Add #deny:equals:description:, #deny:identical:, and #deny:identical:description: - Revise default description message for #deny:equals:, aligning it to #assert:equals: description - Make equality assertions capable of lazy descriptions (aStringOrBlock), aligning them to #assert:description: etc. - Add multilingual support for default description messages - Some internal refactoring - Improve test coverage of these assertions in SUnitTest Thanks to Marcel for pointing to the idea! =============== Diff against SUnit-mt.121 =============== Item was added: + ----- Method: SUnitTest>>testAssertEquals (in category 'tests') ----- + testAssertEquals + + | a b | + a := 'foo'. + b := 'bar'. + + self shouldnt: [self assert: a equals: a copy] raise: TestFailure. + + self should: [self assert: a equals: b] raise: TestFailure. + + [self assert: a equals: b] + on: TestFailure do: [:ex | + | error | + error := ex messageText. + self + assert: (error includesSubstring: a) + description: 'Error message doesn''t include the expected value'. + self + assert: (error includesSubstring: b) + description: 'Error message doesn''t include the actual value'].! Item was added: + ----- Method: SUnitTest>>testAssertEqualsDescription (in category 'tests') ----- + testAssertEqualsDescription + + | a b called | + a := 'foo'. + b := 'bar'. + + self shouldnt: [self assert: a equals: a copy description: 'A description42'] raise: TestFailure. + + self should: [self assert: a equals: b description: 'A description42'] raise: TestFailure. + + [self assert: a equals: b description: 'A description42'] + on: TestFailure do: [:ex | + self + assert: (ex messageText includesSubstring: 'A description42') + description: 'Error message doesn''t give you the description']. + + called := false. + self shouldnt: [self assert: a equals: a description: [called := true]] raise: TestFailure. + self deny: called description: 'Description block was evaluated prematurely'. + + [self assert: a equals: b description: ['A generated description' asUppercase]] + on: TestFailure do: [:ex | + self + assert: (ex messageText includesSubstring: 'A generated description' asUppercase) + description: 'Error message doesn''t give you the generated description'].! Item was changed: ----- Method: SUnitTest>>testAssertIdentical (in category 'tests') ----- testAssertIdentical + | a b | a := 'foo'. b := 'bar'. + + self shouldnt: [self assert: a identical: a] raise: TestFailure. + self should: [self assert: a identical: b] raise: TestFailure. + + [self assert: a identical: b] + on: TestFailure do: [:ex | + | error | + error := ex messageText. + self + assert: (error includesSubstring: a) + description: 'Error message doesn''t include the expected value'. + self + assert: (error includesSubstring: b) + description: 'Error message doesn''t include the actual value'].! - [self assert: a identical: b] on: TestFailure do: [:e | |error| - error := e messageText. - self assert: (error includesSubstring: a) description: 'Error message doesn''t include the expected value'. - self assert: (error includesSubstring: b) description: 'Error message doesn''t include the expected value'].! Item was changed: ----- Method: SUnitTest>>testAssertIdenticalDescription (in category 'tests') ----- testAssertIdenticalDescription + + | a b called | - | a b | a := 'foo'. b := a copy. + + self shouldnt: [self assert: a identical: a description: 'A description42'] raise: TestFailure. + + self should: [self assert: a identical: b description: 'A description42'] raise: TestFailure. + + [self assert: a identical: b description: 'A description42'] + on: TestFailure do: [:ex | + self + assert: (ex messageText includesSubstring: 'A description42') + description: 'Error message doesn''t give you the description']. + + called := false. + self shouldnt: [self assert: a identical: a description: [called := true]] raise: TestFailure. + self deny: called description: 'Description block was evaluated prematurely'. + + [self assert: a identical: b description: ['A generated description' asUppercase]] + on: TestFailure do: [:ex | + self + assert: (ex messageText includesSubstring: 'A generated description' asUppercase) + description: 'Error message doesn''t give you the generated description'].! - self should: [self assert: a identical: b description: 'A desciption'] raise: TestFailure. - [self assert: a identical: b description: 'A desciption'] on: TestFailure do: [:e | |error| - error := e messageText. - self assert: (error includesSubstring: 'A desciption') description: 'Error message doesn''t give you the description'].! Item was changed: ----- Method: SUnitTest>>testAssertIdenticalWithEqualObjects (in category 'tests') ----- testAssertIdenticalWithEqualObjects + | a b | a := 'foo'. b := a copy. + self should: [self assert: a identical: b] raise: TestFailure. + + [self assert: a identical: b] + on: TestFailure do: [:ex | + self + assert: (ex messageText includesSubstring: 'not identical') + description: 'Error message doesn''t say the two things aren''t identical'].! - [self assert: a identical: b] on: TestFailure do: [:e | |error| - error := e messageText. - self assert: (error includesSubstring: 'not identical') description: 'Error message doesn''t say the two things aren''t identical'].! Item was added: + ----- Method: SUnitTest>>testDenyEquals (in category 'tests') ----- + testDenyEquals + + | a b | + a := 'foo'. + b := 'bar'. + + self shouldnt: [self deny: a equals: b] raise: TestFailure. + + self should: [self deny: a equals: a copy] raise: TestFailure. + + [self deny: a equals: a] + on: TestFailure do: [:ex | + self + assert: (ex messageText includesSubstring: a) + description: 'Error message doesn''t include the unexpected value'].! Item was added: + ----- Method: SUnitTest>>testDenyEqualsDescription (in category 'tests') ----- + testDenyEqualsDescription + + | a b called | + a := 'foo'. + b := 'bar'. + + self shouldnt: [self deny: a equals: b description: 'A description42'] raise: TestFailure. + + self should: [self deny: a equals: a copy description: 'A description42'] raise: TestFailure. + + [self deny: a equals: a description: 'A description42'] + on: TestFailure do: [:ex | + self + assert: (ex messageText includesSubstring: 'A description42') + description: 'Error message doesn''t give you the description']. + + called := false. + self shouldnt: [self deny: a equals: b description: [called := true]] raise: TestFailure. + self deny: called description: 'Description block was evaluated prematurely'. + + [self deny: a equals: a description: ['A generated description' asUppercase]] + on: TestFailure do: [:ex | + self + assert: (ex messageText includesSubstring: 'A generated description' asUppercase) + description: 'Error message doesn''t give you the generated description'].! Item was added: + ----- Method: SUnitTest>>testDenyIdentical (in category 'tests') ----- + testDenyIdentical + + | a b | + a := 'foo'. + b := 'bar'. + self shouldnt: [self deny: a identical: b] raise: TestFailure. + self should: [self deny: a identical: a] raise: TestFailure. + [self deny: a identical: a] + on: TestFailure do: [:ex | + self + assert: (ex messageText includesSubstring: a) + description: 'Error message doesn''t include the unexpected value'].! Item was added: + ----- Method: SUnitTest>>testDenyIdenticalDescription (in category 'tests') ----- + testDenyIdenticalDescription + + | a b called | + a := 'foo'. + b := a copy. + + self shouldnt: [self deny: a identical: b description: 'A description42'] raise: TestFailure. + + self should: [self deny: a identical: a description: 'A description42'] raise: TestFailure. + + [self deny: a identical: a description: 'A description42'] + on: TestFailure do: [:ex | + self + assert: (ex messageText includesSubstring: 'A description42') + description: 'Error message doesn''t give you the description']. + + called := false. + self shouldnt: [self deny: a identical: b description: [called := true]] raise: TestFailure. + self deny: called description: 'Description block was evaluated prematurely'. + + [self deny: a identical: a description: ['A generated description' asUppercase]] + on: TestFailure do: [:ex | + self + assert: (ex messageText includesSubstring: 'A generated description' asUppercase) + description: 'Error message doesn''t give you the description'].! Item was added: + ----- Method: SUnitTest>>testDenyIdenticalWithEqualObjects (in category 'tests') ----- + testDenyIdenticalWithEqualObjects + + | a b | + a := 'foo'. + b := a copy. + self should: [self deny: a identical: a] raise: TestFailure. + [self deny: a identical: a] + on: TestFailure do: [:ex | + self + assert: (ex messageText includesSubstring: 'identical') + description: 'Error message doesn''t say the two things are identical'].! Item was changed: ----- Method: TestCase>>assert:equals: (in category 'accessing') ----- assert: expected equals: actual + ^ self + assert: expected + equals: actual + description: nil - ^self - assert: expected = actual - description: [ self comparingStringBetween: expected and: actual ] ! Item was changed: ----- Method: TestCase>>assert:equals:description: (in category 'accessing') ----- + assert: expected equals: actual description: aStringOrBlock - assert: expected equals: actual description: aString + ^ self - ^self assert: expected = actual + description: [self + description: aStringOrBlock + with: (self comparingStringBetween: expected and: actual)]! - description: [ aString , ': ', (self comparingStringBetween: expected and: actual) ]! Item was changed: ----- Method: TestCase>>assert:identical: (in category 'accessing') ----- assert: expected identical: actual + ^ self + assert: expected + identical: actual + description: nil! - ^self - assert: expected == actual - description: [ self comparingStringBetweenIdentical: expected and: actual ] - ! Item was changed: ----- Method: TestCase>>assert:identical:description: (in category 'accessing') ----- + assert: expected identical: actual description: aStringOrBlock - assert: expected identical: actual description: aString + ^ self - ^self assert: expected == actual + description: [self + description: aStringOrBlock + with: (self comparingStringBetween: expected andIdentical: actual)]! - description: [ aString , ': ', (self comparingStringBetweenIdentical: expected and: actual) ]! Item was changed: ----- Method: TestCase>>comparingStringBetween:and: (in category 'private') ----- comparingStringBetween: expected and: actual + + ^ 'Expected {1} but was {2}.' translated + format: { + expected printStringLimitedTo: 10. + actual printStringLimitedTo: 10 }! - ^ String streamContents: [:stream | - stream - nextPutAll: 'Expected '; - nextPutAll: (expected printStringLimitedTo: 10); - nextPutAll: ' but was '; - nextPutAll: (actual printStringLimitedTo: 10); - nextPutAll: '.' - ]! Item was added: + ----- Method: TestCase>>comparingStringBetween:andIdentical: (in category 'private') ----- + comparingStringBetween: expected andIdentical: actual + + ^ 'Expected {1} and actual {2} are not identical.' translated + format: { + expected printStringLimitedTo: 10. + actual printStringLimitedTo: 10 }! Item was changed: ----- Method: TestCase>>comparingStringBetweenIdentical:and: (in category 'private') ----- comparingStringBetweenIdentical: expected and: actual + + self deprecated. + ^ self comparingStringBetween: expected andIdentical: actual! - ^ 'Expected {1} and actual {2} are not identical.' format: { - expected printStringLimitedTo: 10. - actual printStringLimitedTo: 10. - }! Item was added: + ----- Method: TestCase>>comparingStringBetweenUnexpected:and: (in category 'private') ----- + comparingStringBetweenUnexpected: unexpected and: actual + + ^ 'Did not expect {1} but was {2}.' translated + format: { + unexpected printStringLimitedTo: 10. + actual printStringLimitedTo: 10 }! Item was added: + ----- Method: TestCase>>comparingStringBetweenUnexpected:andIdentical: (in category 'private') ----- + comparingStringBetweenUnexpected: expected andIdentical: actual + + ^ 'Unexpected {1} and actual {2} are identical.' translated + format: { + expected printStringLimitedTo: 10. + actual printStringLimitedTo: 10 }! Item was changed: ----- Method: TestCase>>deny:equals: (in category 'accessing') ----- deny: unexpected equals: actual + ^ self + deny: unexpected + equals: actual + description: nil! - ^self - deny: unexpected = actual - description: 'Actual equals unexpected' - ! Item was added: + ----- Method: TestCase>>deny:equals:description: (in category 'accessing') ----- + deny: unexpected equals: actual description: aStringOrBlock + + ^ self + deny: unexpected = actual + description: [self + description: aStringOrBlock + with: (self comparingStringBetweenUnexpected: unexpected and: actual)]! Item was added: + ----- Method: TestCase>>deny:identical: (in category 'accessing') ----- + deny: unexpected identical: actual + + ^ self + deny: unexpected + identical: actual + description: nil! Item was added: + ----- Method: TestCase>>deny:identical:description: (in category 'accessing') ----- + deny: unexpected identical: actual description: aStringOrBlock + + ^ self + deny: unexpected == actual + description: [self + description: aStringOrBlock + with: (self comparingStringBetweenUnexpected: unexpected andIdentical: actual)]! Item was added: + ----- Method: TestCase>>description:with: (in category 'private') ----- + description: aStringOrBlock with: reason + + | description | + description := aStringOrBlock value. + ^ description + ifNil: [reason] + ifNotNil: ['{1}: {2}' translated format: {description. reason}]! |
Free forum by Nabble | Edit this page |