Skip to content

Instantly share code, notes, and snippets.

@dbousamra
Created February 6, 2015 03:24
Show Gist options
  • Select an option

  • Save dbousamra/9d700331f29cca533bad to your computer and use it in GitHub Desktop.

Select an option

Save dbousamra/9d700331f29cca533bad to your computer and use it in GitHub Desktop.
var _ = require('lodash')
var mocha = require('mocha')
var should = require('should')
var testcheck = require('testcheck')
var mochaCheck = require('mocha-testcheck').install();
var gen = testcheck.gen
var DomsStringUtils = {
truncateBetter: function(input, length) {
if (length < 0) {
return ''
} else if (input.length <= length) {
return input;
} else {
return input.substring(0, length) + '...';
}
},
truncateOrig: function(input, length) {
if (input.length <= length) {
return input;
} else {
return input.substring(0, length) + '...';
}
},
containsOrig: function(s, substring) {
return s.indexOf(substring) != -1;
}
}
describe('DomsStringUtils using "TDD style"', function() {
describe('#truncate', function() {
it('Should truncate a short string', function(done) {
DomsStringUtils.truncateOrig('abc', 5).should.eql('abc')
done()
})
it('Should truncate a long string', function(done) {
DomsStringUtils.truncateOrig('Hello World', 8).should.eql('Hello Wo...')
done()
})
})
describe('#contains', function() {
it('Should return true if a string contains another one', function(done) {
DomsStringUtils.containsOrig('abc', 'bc').should.be.true
done()
})
it('Should return false if a string doesnt contains another one', function(done) {
DomsStringUtils.containsOrig('abc', '42').should.be.false
done()
})
})
})
describe('DomsStringUtils using "Property based testing style"', function() {
describe('#truncate', function() {
check.it('Can handle negative lengths', [gen.alphaNumString, gen.int], function (input, length) {
var truncated = DomsStringUtils.truncateBetter(input, length);
if (length < 0)
truncated.should.equal('')
})
check.it('Can handle input smaller than lengths', [gen.alphaNumString, gen.int], function (input, length) {
var truncated = DomsStringUtils.truncateBetter(input, length);
if (input.length <= length)
truncated.should.equal(input)
})
check.it('Can handle input greater than lengths', [gen.alphaNumString, gen.int], function (input, length) {
var truncated = DomsStringUtils.truncateBetter(input, length);
if (input.length > length && length > 0)
truncated.should.equal(input.substring(0, length) + '...')
})
})
describe('#contains', function() {
check.it('Testing if a string comprising 3 strings, contains each of those individually.', [gen.alphaNumString, gen.alphaNumString, gen.alphaNumString], function (s1, s2, s3) {
var inputStr = s1 + s2 + s3
DomsStringUtils.containsOrig(inputStr, s1)
DomsStringUtils.containsOrig(inputStr, s2)
DomsStringUtils.containsOrig(inputStr, s3)
})
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment