From 78b5e60805c8ae175989341e24530f858b1d474a Mon Sep 17 00:00:00 2001 From: David Chambers Date: Tue, 10 Apr 2012 00:32:47 -0700 Subject: [PATCH] rewrite tests with jessie --- README.md | 4 +- package.json | 28 ++++++---- spec/spec_helper.js | 2 + spec/stringformat_spec.coffee | 92 +++++++++++++++++++++++++++++++++ tests.coffee | 97 ----------------------------------- 5 files changed, 115 insertions(+), 108 deletions(-) create mode 100644 spec/spec_helper.js create mode 100644 spec/stringformat_spec.coffee delete mode 100644 tests.coffee diff --git a/README.md b/README.md index cab6412..5421985 100644 --- a/README.md +++ b/README.md @@ -137,8 +137,8 @@ greet("Steve", 1) ### Running the test suite - $ coffee tests.coffee - 37 of 37 tests passed + npm install + npm test [1]: http://docs.python.org/library/stdtypes.html#str.format diff --git a/package.json b/package.json index 65a43bd..a4c67ab 100644 --- a/package.json +++ b/package.json @@ -1,11 +1,21 @@ { - "name" : "string-format", - "description" : "Adds a `format` method to `String.prototype`. Inspired by Python's `str.format()`.", - "keywords" : ["string", "formatting", "language", "util"], - "homepage" : "https://github.com/davidchambers/string-format", - "bugs" : "https://github.com/davidchambers/string-format/issues", - "repository" : {"type": "git", "url": "https://github.com/davidchambers/string-format"}, - "author" : "David Chambers ", - "main" : "string-format.js", - "version" : "0.1.0" + "name": "string-format", + "description": "Adds a `format` method to `String.prototype`. Inspired by Python's `str.format()`.", + "keywords": ["string", "formatting", "language", "util"], + "homepage": "https://github.com/davidchambers/string-format", + "bugs": "https://github.com/davidchambers/string-format/issues", + "repository": { + "type": "git", + "url": "https://github.com/davidchambers/string-format" + }, + "author": "David Chambers ", + "main": "string-format.js", + "version": "0.1.0", + "devDependencies": { + "coffee-script": "1.2.x", + "jessie": "0.4.x" + }, + "scripts": { + "test": "node_modules/jessie/bin/jessie spec" + } } diff --git a/spec/spec_helper.js b/spec/spec_helper.js new file mode 100644 index 0000000..6bf8ea3 --- /dev/null +++ b/spec/spec_helper.js @@ -0,0 +1,2 @@ +require('coffee-script') +require('jessie').sugar() diff --git a/spec/stringformat_spec.coffee b/spec/stringformat_spec.coffee new file mode 100644 index 0000000..dc14d8f --- /dev/null +++ b/spec/stringformat_spec.coffee @@ -0,0 +1,92 @@ +require '../string-format' + +describe 'String::format', -> + + it 'interpolates positional arguments', -> + '{0}, you have {1} unread message{2}'.format('Holly', 2, 's') + .should_be 'Holly, you have 2 unread messages' + + it 'strips unmatched placeholders', -> + '{0}, you have {1} unread message{2}'.format('Steve', 1) + .should_be 'Steve, you have 1 unread message' + + it 'allows indexes to be omitted if they are entirely sequential', -> + '{}, you have {} unread message{}'.format('Steve', 1) + .should_be 'Steve, you have 1 unread message' + + it 'replaces all occurrences of a placeholder', -> + 'the meaning of life is {0} ({1} x {2} is also {0})'.format(42, 6, 7) + .should_be 'the meaning of life is 42 (6 x 7 is also 42)' + + it 'creates a reusable template function when invoked with no arguments', -> + explicit = '{0}, you have {1} unread message{2}'.format() + implicit = '{}, you have {} unread message{}'.format() + explicit('Holly', 2, 's').should_be 'Holly, you have 2 unread messages' + implicit('Holly', 2, 's').should_be 'Holly, you have 2 unread messages' + explicit('Steve', 1).should_be 'Steve, you have 1 unread message' + implicit('Steve', 1).should_be 'Steve, you have 1 unread message' + + it 'does not allow explicit and implicit numbering to be intermingled', -> + expect(-> '{} {0}'.format 'foo', 'bar') + .toThrow 'cannot switch from implicit to explicit numbering' + expect(-> '{1} {}'.format 'foo', 'bar') + .toThrow 'cannot switch from explicit to implicit numbering' + expect(-> '{1} {}'.format() 'foo', 'bar') + .toThrow 'cannot switch from explicit to implicit numbering' + + it 'treats "{{" and "}}" as "{" and "}"', -> + '{{ {}: "{}" }}'.format('foo', 'bar').should_be '{ foo: "bar" }' + + it 'supports property access via dot notation', -> + bobby = first_name: 'Bobby', last_name: 'Fischer' + garry = first_name: 'Garry', last_name: 'Kasparov' + '{0.first_name} {0.last_name} vs. {1.first_name} {1.last_name}'.format(bobby, garry) + .should_be 'Bobby Fischer vs. Garry Kasparov' + + it 'accepts a shorthand for properties of the first positional argument', -> + bobby = first_name: 'Bobby', last_name: 'Fischer' + '{first_name} {last_name}'.format(bobby).should_be 'Bobby Fischer' + + String::format.transformers.s = -> 's' unless +this is 1 + + it 'applies transformers to explicit positional arguments', -> + text = '{0}, you have {1} unread message{1!s}' + text.format('Steve', 1).should_be 'Steve, you have 1 unread message' + text.format('Holly', 2).should_be 'Holly, you have 2 unread messages' + + it 'applies transformers to implicit positional arguments', -> + text = 'The Cure{!s}, The Door{!s}, The Smith{!s}' + text.format(1, 2, 3).should_be 'The Cure, The Doors, The Smiths' + + it 'applies transformers to properties of explicit positional arguments', -> + text = 'view message{0.length!s}' + text.format(new Array 1).should_be 'view message' + text.format(new Array 2).should_be 'view messages' + + it 'applies transformers to properties of implicit positional arguments', -> + text = 'view message{length!s}' + text.format(new Array 1).should_be 'view message' + text.format(new Array 2).should_be 'view messages' + + it "passes applicable tests from Python's test suite", -> + ''.format(null).should_be '' + 'abc'.format(null).should_be 'abc' + '{0}'.format('abc').should_be 'abc' + 'X{0}'.format('abc').should_be 'Xabc' + '{0}X'.format('abc').should_be 'abcX' + 'X{0}Y'.format('abc').should_be 'XabcY' + '{1}'.format(1, 'abc').should_be 'abc' + 'X{1}'.format(1, 'abc').should_be 'Xabc' + '{1}X'.format(1, 'abc').should_be 'abcX' + 'X{1}Y'.format(1, 'abc').should_be 'XabcY' + '{0}'.format(-15).should_be '-15' + '{0}{1}'.format(-15, 'abc').should_be '-15abc' + '{0}X{1}'.format(-15, 'abc').should_be '-15Xabc' + '{{'.format(null).should_be '{' + '}}'.format(null).should_be '}' + '{{}}'.format(null).should_be '{}' + '{{x}}'.format(null).should_be '{x}' + '{{{0}}}'.format(123).should_be '{123}' + '{{{{0}}}}'.format(null).should_be '{{0}}' + '}}{{'.format(null).should_be '}{' + '}}x{{'.format(null).should_be '}x{' diff --git a/tests.coffee b/tests.coffee deleted file mode 100644 index 55565c8..0000000 --- a/tests.coffee +++ /dev/null @@ -1,97 +0,0 @@ -require './string-format' - - -count = passes = 0 - -ok = (actual, expected) -> - count += 1 - passes += 1 if actual is expected - -throws = (fn, expected_error) -> - count += 1 - try - do fn - catch error - passes += 1 if error is expected_error - - -ok '{0}, you have {1} unread message{2}'.format('Holly', 2, 's') - , 'Holly, you have 2 unread messages' - -ok '{0}, you have {1} unread message{2}'.format('Steve', 1) - , 'Steve, you have 1 unread message' - -ok 'the meaning of life is {0} ({1} x {2} is also {0})'.format(42, 6, 7) - , 'the meaning of life is 42 (6 x 7 is also 42)' - -ok '{}, you have {} unread message{}'.format('Steve', 1) - , 'Steve, you have 1 unread message' - -throws (-> '{} {0}'.format 'foo', 'bar') - , 'cannot switch from implicit to explicit numbering' - -throws (-> '{1} {}'.format 'foo', 'bar') - , 'cannot switch from explicit to implicit numbering' - -template = '{1} {}'.format() - -throws (-> template 'foo', 'bar') - , 'cannot switch from explicit to implicit numbering' - -ok '{{ {}: "{}" }}'.format('foo', 'bar') - , '{ foo: "bar" }' - -bobby = first_name: 'Bobby', last_name: 'Fischer' -garry = first_name: 'Garry', last_name: 'Kasparov' - -ok '{0.first_name} {0.last_name} vs. {1.first_name} {1.last_name}'.format(bobby, garry) - , 'Bobby Fischer vs. Garry Kasparov' - -ok '{first_name} {last_name}'.format(bobby) - , 'Bobby Fischer' - -String::format.transformers.s = -> 's' unless +this is 1 - -ok '{0}, you have {1} unread message{1!s}'.format('Holly', 2) - , 'Holly, you have 2 unread messages' - -ok '{0}, you have {1} unread message{1!s}'.format('Steve', 1) - , 'Steve, you have 1 unread message' - -ok 'view message{!s}'.format(2) - , 'view messages' - -ok 'view message{!s}'.format(1) - , 'view message' - -ok 'view message{length!s}'.format(['foo', 'bar']) - , 'view messages' - -ok 'view message{length!s}'.format(['baz']) - , 'view message' - -# From Python's test suite: -ok ''.format(null), '' -ok 'abc'.format(null), 'abc' -ok '{0}'.format('abc'), 'abc' -ok 'X{0}'.format('abc'), 'Xabc' -ok '{0}X'.format('abc'), 'abcX' -ok 'X{0}Y'.format('abc'), 'XabcY' -ok '{1}'.format(1, 'abc'), 'abc' -ok 'X{1}'.format(1, 'abc'), 'Xabc' -ok '{1}X'.format(1, 'abc'), 'abcX' -ok 'X{1}Y'.format(1, 'abc'), 'XabcY' -ok '{0}'.format(-15), '-15' -ok '{0}{1}'.format(-15, 'abc'), '-15abc' -ok '{0}X{1}'.format(-15, 'abc'), '-15Xabc' -ok '{{'.format(null), '{' -ok '}}'.format(null), '}' -ok '{{}}'.format(null), '{}' -ok '{{x}}'.format(null), '{x}' -ok '{{{0}}}'.format(123), '{123}' -ok '{{{{0}}}}'.format(null), '{{0}}' -ok '}}{{'.format(null), '}{' -ok '}}x{{'.format(null), '}x{' - - -console.log "#{passes} of #{count} tests passed"