Permalink
Browse files

updating licence and makefile

  • Loading branch information...
1 parent 3bfa2bc commit 1a4296b71cada84074540205fb386656506b192b @hyperthunk committed May 27, 2010
Showing with 217 additions and 58 deletions.
  1. +1 −1 LICENCE
  2. +25 −0 Makefile
  3. +0 −14 NOTES
  4. +51 −40 README
  5. +26 −1 ebin/hamcrest.app
  6. +30 −0 hrlgen
  7. +25 −0 include/test.hrl
  8. +30 −1 src/hamcrest_matchers.erl
  9. +29 −1 test/hamcrest_matchers_SUITE.erl
View
@@ -1,4 +1,4 @@
-Copyright (c) 2008, Tim Watson
+Copyright (c) 2010, Tim Watson
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
View
@@ -1,3 +1,28 @@
+# -----------------------------------------------------------------------------
+#
+# Hamcrest Erlang.
+#
+# Copyright (c) 2010 Tim Watson (watson.timothy@gmail.com)
+#
+# Permission is hereby granted, free of charge, to any person obtaining a copy
+# of this software and associated documentation files (the "Software"), to deal
+# in the Software without restriction, including without limitation the rights
+# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+# copies of the Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice shall be included in
+# all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+# THE SOFTWARE.
+# -----------------------------------------------------------------------------
+
ERL ?= `which erl`
ERL_LIBS := $(shell echo "./deps:`echo $$ERL_LIBS`")
VERBOSE ?= ""
View
@@ -1,15 +1 @@
-js libs:
- http://raphaeljs.com/ -- appears to have a few bugs on safari, nice quality vg though
- http://g.raphaeljs.com/ -- more feature complete but complex
- http://code.google.com/p/flot/ -- nice simple API for this one
- http://bluff.jcoglan.com/api.html -- looks good, will read from html tables!
- http://www.filamentgroup.com/lab/update_to_jquery_visualize_accessible_charts_with_html5_from_designing_with/
- -- has the same idea as bluff but is jQuery based on accessible
-
-
-commet approaches:
-
- http://code.google.com/p/erlycomet/source/checkout
- http://www.metabrew.com/article/tag/comet
-
View
@@ -1,4 +1,7 @@
-Introduction
+Hamcrest Erlang
+=============================
+
+This is an implementation of Hamcrest for the [Erlang programming language](http://www.erlang.org/).
Hamcrest is a framework for writing matcher objects allowing 'match' rules to be defined declaratively.
There are a number of situations where matchers are invaluble, such as UI validation, or data filtering,
@@ -13,32 +16,35 @@ Such tests fail when the behaviour of the aspect under test deviates from the ex
to pass when minor, unrelated changes to the behaviour are made.
My first Hamcrest test
+------------------------
We'll start be writing a very simple EUnit test, but instead of using EUnit's ?assertEqual macro, we use
Hamcrest's assert_that construct and the standard set of matchers:
-from hamcrest import *
-import unittest
+ from hamcrest import *
+ import unittest
+
+ class BiscuitTest(unittest.TestCase):
+ def testEquals(self):
+ theBiscuit = Biscuit('Ginger')
+ myBiscuit = Biscuit('Ginger')
+ assert_that(theBiscuit, equal_to(myBiscuit))
-class BiscuitTest(unittest.TestCase):
- def testEquals(self):
- theBiscuit = Biscuit('Ginger')
- myBiscuit = Biscuit('Ginger')
- assert_that(theBiscuit, equal_to(myBiscuit))
+ if __name__ == '__main__':
+ unittest.main()
-if __name__ == '__main__':
- unittest.main()
The assert_that function is a stylized sentence for making a test assertion. In this example, the subject of the
assertion is the object theBiscuit that is the first method parameter. The second method parameter is a matcher
for Biscuit objects, here a matcher that checks one object is equal to another using the Python == operator.
The test passes since the Biscuit class defines an eq method.
If you have more than one assertion in your test you can include an identifier for the tested value in the assertion:
-assert_that(theBiscuit.getChocolateChipCount(), equal_to(10), 'chocolate chips')
-assert_that(theBiscuit.getHazelnutCount(), equal_to(3), 'hazelnuts')
+ assert_that(theBiscuit.getChocolateChipCount(), equal_to(10), 'chocolate chips')
+ assert_that(theBiscuit.getHazelnutCount(), equal_to(3), 'hazelnuts')
A tour of common matchers
+-----------------------------
Hamcrest comes with a library of useful matchers. Here are some of the most important ones.
@@ -72,12 +78,14 @@ Syntactic sugar
Hamcrest strives to make your tests as readable as possible. For example, the is_ matcher is a wrapper that doesn't
add any extra behavior to the underlying matcher. The following assertions are all equivalent:
-assert_that(theBiscuit, equal_to(myBiscuit))
-assert_that(theBiscuit, is_(equal_to(myBiscuit)))
-assert_that(theBiscuit, is_(myBiscuit))
+ assert_that(theBiscuit, equal_to(myBiscuit))
+ assert_that(theBiscuit, is_(equal_to(myBiscuit)))
+ assert_that(theBiscuit, is_(myBiscuit))
+
The last form is allowed since is_(value) wraps non-matcher arguments with equal_to.
Writing custom matchers
+----------------------------
Hamcrest comes bundled with lots of useful matchers, but you'll probably find that you need to create your own from
time to time to fit your testing needs. This commonly occurs when you find a fragment of code that tests the same set
@@ -86,48 +94,51 @@ By writing your own matcher you'll eliminate code duplication and make your test
Let's write our own matcher for testing if a string is comprised of only digits. This is the test we want to write:
-def testStringIsOnlyDigits(self):
- assert_that('17', is_(only_digits()))
-}
+ def testStringIsOnlyDigits(self):
+ assert_that('17', is_(only_digits()))
+
And here's the implementation:
-from hamcrest.core.base_matcher import BaseMatcher
-from hamcrest.core.internal.hasmethod import hasmethod
+ from hamcrest.core.base_matcher import BaseMatcher
+ from hamcrest.core.internal.hasmethod import hasmethod
-class IsStringOfDigits(BaseMatcher):
+ class IsStringOfDigits(BaseMatcher):
- def _matches(self, item):
- if not hasmethod(item, 'isdigit'):
- return False
- return item.isdigit()
+ def _matches(self, item):
+ if not hasmethod(item, 'isdigit'):
+ return False
+ return item.isdigit()
- def describe_to(self, description):
- description.append_text('string of digits')
+ def describe_to(self, description):
+ description.append_text('string of digits')
+
+ only_digits = IsStringOfDigits
-only_digits = IsStringOfDigits
For our Matcher implementation we implement the _matches method - which calls the isdigit string method after confirming
that the argument (which may not be a string) has such a method - and the describe_to method - which is used to produce a
failure message when a test fails. Here's an example of how the failure message looks:
-assert_that('17b', is_(only_digits()))
+ assert_that('17b', is_(only_digits()))
+
fails with the message
-AssertionError:
-Expected: is string of digits
- got: '17b'
+ AssertionError:
+ Expected: is string of digits
+ got: '17b'
+
The factory function only_digits is defined as an alias because no further logic is needed, but other matchers may work best
with an actual factory. We import only_digits to use the matcher in our test:
-from hamcrest import *
-import unittest
-from hamcrest_local.isstringofdigits import only_digits
+ from hamcrest import *
+ import unittest
+ from hamcrest_local.isstringofdigits import only_digits
-class StringTest(unittest.TestCase):
- def testStringIsOnlyDigits(self):
- assert_that('17', is_(only_digits()))
+ class StringTest(unittest.TestCase):
+ def testStringIsOnlyDigits(self):
+ assert_that('17', is_(only_digits()))
-if __name__ == '__main__':
- unittest.main()
+ if __name__ == '__main__':
+ unittest.main()
Even though the only_digits function creates a new matcher each time it is called, you should not assume this is the only usage
pattern for your matcher. Therefore you should make sure your matcher is stateless, so a single instance can be reused between matches.
View
@@ -1,8 +1,33 @@
+%% -----------------------------------------------------------------------------
+%%
+%% Hamcrest Erlang.
+%%
+%% Copyright (c) 2010 Tim Watson (watson.timothy@gmail.com)
+%%
+%% Permission is hereby granted, free of charge, to any person obtaining a copy
+%% of this software and associated documentation files (the "Software"), to deal
+%% in the Software without restriction, including without limitation the rights
+%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+%% copies of the Software, and to permit persons to whom the Software is
+%% furnished to do so, subject to the following conditions:
+%%
+%% The above copyright notice and this permission notice shall be included in
+%% all copies or substantial portions of the Software.
+%%
+%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+%% THE SOFTWARE.
+%% -----------------------------------------------------------------------------
+
{application,
hamcrest,
[{description,"Hamcrest Matcher Library"},
{vsn,"0.0.1"},
{modules,[hamcrest_matchers]},
{registered,[]},
{applications,[kernel]},
- {env,[]}]}.
+ {env,[]}]}.
View
@@ -1,5 +1,35 @@
#!/usr/bin/env escript
+%% -----------------------------------------------------------------------------
+%%
+%% Hamcrest Erlang.
+%%
+%% Copyright (c) 2010 Tim Watson (watson.timothy@gmail.com)
+%%
+%% Permission is hereby granted, free of charge, to any person obtaining a copy
+%% of this software and associated documentation files (the "Software"), to deal
+%% in the Software without restriction, including without limitation the rights
+%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+%% copies of the Software, and to permit persons to whom the Software is
+%% furnished to do so, subject to the following conditions:
+%%
+%% The above copyright notice and this permission notice shall be included in
+%% all copies or substantial portions of the Software.
+%%
+%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+%% THE SOFTWARE.
+%% -----------------------------------------------------------------------------
+%% @author Tim Watson <watson.timothy@gmail.com>
+%% @copyright 2010 Tim Watson.
+%%
+%% Generates hamcrest.hrl header file during build process.
+%% -----------------------------------------------------------------------------
+
main(_) ->
code:add_patha("ebin"),
Exports = lists:filter(
View
@@ -1,5 +1,30 @@
+%% -----------------------------------------------------------------------------
+%%
+%% Hamcrest Erlang.
+%%
+%% Copyright (c) 2010 Tim Watson (watson.timothy@gmail.com)
+%%
+%% Permission is hereby granted, free of charge, to any person obtaining a copy
+%% of this software and associated documentation files (the "Software"), to deal
+%% in the Software without restriction, including without limitation the rights
+%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+%% copies of the Software, and to permit persons to whom the Software is
+%% furnished to do so, subject to the following conditions:
+%%
+%% The above copyright notice and this permission notice shall be included in
+%% all copies or substantial portions of the Software.
+%%
+%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+%% THE SOFTWARE.
+%% -----------------------------------------------------------------------------
%% @author Tim Watson <watson.timothy@gmail.com>
%% @copyright 2008 Tim Watson.
+%% -----------------------------------------------------------------------------
-define(CT_REGISTER_TESTS(Mod),
All = [ FName || {FName, _} <- lists:filter(
@@ -1,5 +1,33 @@
+%% -----------------------------------------------------------------------------
+%%
+%% Hamcrest Erlang.
+%%
+%% Copyright (c) 2010 Tim Watson (watson.timothy@gmail.com)
+%%
+%% Permission is hereby granted, free of charge, to any person obtaining a copy
+%% of this software and associated documentation files (the "Software"), to deal
+%% in the Software without restriction, including without limitation the rights
+%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+%% copies of the Software, and to permit persons to whom the Software is
+%% furnished to do so, subject to the following conditions:
+%%
+%% The above copyright notice and this permission notice shall be included in
+%% all copies or substantial portions of the Software.
+%%
+%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+%% THE SOFTWARE.
+%% -----------------------------------------------------------------------------
%% @author Tim Watson <watson.timothy@gmail.com>
-%% @copyright 2008 Tim Watson.
+%% @copyright 2010 Tim Watson.
+%% @doc Hamcrest Matchers
+%% @reference See <a href="http://code.google.com/p/hamcrest/">Hamcrest</a>
+%% for more information.
+%% -----------------------------------------------------------------------------
-module(hamcrest_matchers).
-author('Tim Watson <watson.timothy@gmail.com>').
@@ -23,6 +51,7 @@
anything() ->
fun(_) -> true end.
+-spec(any_of/1 :: (list(fun((term()) -> boolean()))) -> fun((term()) -> boolean())).
any_of(Matchers) when is_list(Matchers) ->
fun(X) -> lists:member(true, [ M(X) || M <- Matchers ]) end.
@@ -1,5 +1,33 @@
+%% -----------------------------------------------------------------------------
+%%
+%% Hamcrest Erlang.
+%%
+%% Copyright (c) 2010 Tim Watson (watson.timothy@gmail.com)
+%%
+%% Permission is hereby granted, free of charge, to any person obtaining a copy
+%% of this software and associated documentation files (the "Software"), to deal
+%% in the Software without restriction, including without limitation the rights
+%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+%% copies of the Software, and to permit persons to whom the Software is
+%% furnished to do so, subject to the following conditions:
+%%
+%% The above copyright notice and this permission notice shall be included in
+%% all copies or substantial portions of the Software.
+%%
+%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+%% THE SOFTWARE.
+%% -----------------------------------------------------------------------------
%% @author Tim Watson <watson.timothy@gmail.com>
-%% @copyright 2008 Tim Watson.
+%% @copyright 2010 Tim Watson.
+%% @doc Hamcrest Matchers
+%% @reference See <a href="http://code.google.com/p/hamcrest/">Hamcrest</a>
+%% for more information.
+%% -----------------------------------------------------------------------------
%% module annotations
-module(hamcrest_matchers_SUITE).

0 comments on commit 1a4296b

Please sign in to comment.