chicken scheme srfi-64 test-error problems - unit-testing

I am using chicken scheme version 4.9.0.1 and am working on a
unit test suite using the unit test package srfi-64.
I am having trouble with test-error in this unit test package.
A minimal example of the problem:
(require-extension srfi-64)
(test-begin "error testing")
(define (exn-tester)
(error "What does srfi-64 want?"))
(test-error "the exn-tester" 'exn (exn-tester))
(test-end)
Results in the report:
%%%% Starting test error testing
Group begin: error testing
Test begin:
test-name: "the exn-tester"
Test end:
result-kind: fail
actual-error: #<condition: (exn)>
expected-error: exn
Group end: error testing
# of unexpected failures 1
The problem is clearly in my test-error because I don't know how
to represent the exception in a form that test-error understands.
I've tried multiple variations on the actual-error as listed,
but to no success.
Specifically, what should I code for the expected-error in the
test-error call?

The SRFI spec itself basically says "it's implementation-dependent", but after a look at the code it seems you can simply pass it a predicate:
(require-extension srfi-64)
(test-begin "error testing")
(define (exn-tester)
(error "What does srfi-64 want?"))
(test-error "the exn-tester" (condition-predicate 'exn) (exn-tester))
(test-end)
Please note that in the CHICKEN community, the test egg is the facto testing egg. However, it currently has no way to test for a specific exception type. I once wrote this macro for the Postgres egg's test suite:
(define-syntax test-error*
(syntax-rules ()
((_ ?msg (?error-type ...) ?expr)
(let-syntax ((expression:
(syntax-rules ()
((_ ?expr)
(condition-case (begin ?expr "<no error thrown>")
((?error-type ...) '(?error-type ...))
(exn () (##sys#slot exn 1)))))))
(test ?msg '(?error-type ...) (expression: ?expr))))
((_ ?msg ?error-type ?expr)
(test-error* ?msg (?error-type) ?expr))
((_ ?error-type ?expr)
(test-error* (sprintf "~S" '?expr) ?error-type ?expr))))
To be used as follows:
(test-error* "Result value error for out of bounds row"
(exn postgresql bounds)
(value-at (query conn "SELECT NULL") 0 1))
The (exn postgresql bounds) tells the test to accept a composite condition (refer to SRFI-12 for more info) of the types exn, postgresql and bounds. It can have other types, but in this example, these three must be present for the test to pass.

With your tips, the following works well enough:
(require-extension test)
test-begin "error-testing")
(define (exn-tester)
(error "What does srfi-64 want?"))
(test-error (exn-tester))
(test-end "error-testing")
Attempts to use your define-syntax resulted in "unbound variable: exn".
Perhaps I require additional extension to use this?
However, your define-syntax will warrant future attention. I am only getting started in Scheme and, having a test driven background, am most interested in my code being the very best.

Related

Spock invocation test print too few information

Is it possible to make Spock give more information about failed tests.
Below test example only print the name booServices.someFunc when too few invocation. But in this case I would like see the exact name of booServices not the test name.
It is not a problem when too many invocation.
spyServices.each { Class<? extends FooServices> clazz, BooServices booServices ->
(clazz in listOfExpected ? 1I : 0I) * booServices.someFunc(_)
}
This is what Spock print out, and here I would like to know the booServices real name:
Too few invocations for:
1 * booServices.someFunc(_) (0 invocations)

converting for and if statements to list comprehensions in python

I'm trying to incorporate list comprehensions in my code, for ease of understanding's and efficiency's sake.
My code is this:
for tblock in jdata['network']['tcp']:
if not [tblock['dst'], tblock['dport']] in test:
test.append([tblock['dst'], tblock['dport']])
test = [ip for ip in test if not ip[0].startswith(fil2)]
The error I am getting is on the test.append part. Below is just a cobbled together attempt, as I dont know how to bring in the test.append portion.
test = [tblock for tblock in jdata['network']['tcp'] if not [tblock['dst'], tblock['dport']] in test if test > 0 test.append([tblock['dst'], tblock['dport']])]
SyntaxError: invalid syntax
For context, fil2 is a list of IP's that filters against the information. I can provide test data if anyone needs it.

Spock interaction based test does not fail

I have the following two Spock tests:
def "sends a valid response when no users exist"() {
setup:
def exchange = new HttpServerExchange(Mock(ServerConnection))
usersRepository.size() >> 0
when:
firstRunHandler.handleRequest(exchange)
then:
1*response.send(exchange, _)
}
def "does not send content when any users exist"() {
setup:
usersRepository.size() >> 1
when:
firstRunHandler.handleRequest(new HttpServerExchange(Mock(ServerConnection)))
then:
0*response.send(_, _)
}
The second one should definitely fail, since the interaction is stll there. But it always passes. I can not even make it fail with:
then:
assert false
0*response.send(_, _)
IntelliJ Idea still shows it as "green". But when I change the "then" to
then:
assert false
the test fails, so it is definitely being run and executed as a spock test.
I don't get much info from Spock, and I did not find out anything when debugging. What am I missing? What can I do to diagnose this problem?

Grails 1.3.7 to Grails 2.2 upgrade, Unit tests now fail with missing GORM methods

I am in the middle of upgrading an app from Grails 1.3.7 to 2.2
So far, its been relatively painless and straight forward.
Until we started running the unit tests.
Under 1.3.7, all the tests passed.
Under 2.2, about half are now failing. The tests haven't changed, they are still the old style mockDomain...
What is most concerning to me is that basic gorm features are missing on some of the domain classes.
Things like .list and .get
Failure: testList_NoMaxSpecified_10Shown(com.litle.bldvwr.StreamControllerTests)
| groovy.lang.MissingMethodException: No signature of method: >com.litle.bldvwr.Stream.list() is applicable for argument types: () values: []
Possible solutions: list(), list(), list(), list(java.lang.Object), list(java.util.Map), >list(java.lang.Object)
and
Failure: >testAddFailureOutputToHappyPathWithIntegrationFailure(com.litle.bldvwr.LogParserServiceTests)
| groovy.lang.MissingMethodException: No signature of method: >com.litle.bldvwr.Result.get() is applicable for argument types: () values: []
Possible solutions: get(java.io.Serializable), get(java.lang.Object), >get(java.io.Serializable), getId(), grep(), grep(java.lang.Object)
The general pattern of for this type of failure is:
mockDomain(Phase, [new Phase(id:1, name: 'xxx')])
mockDomain(Result, [new Result(id:1, phase: Phase.get(1), failureOutput:"")])
logParserService.addFailureOutputTo(Result.get(1))
And it is that last get that is causing the no signature error.
While we intend to start using the new Unit Test functionality, I was hoping to avoid having to rewrite the 500+ current tests.
Thoughts, ideas?
-Clark
Using the new #Mock() annotation in your test for the domain objects will inject all the expected mock GORM methods, and you can even just save() your domain objects instead of providing the list in mockDomain() call.
#Mock([Result, Nightly])
class MyTests {
void testSomething() {
def night = new Nightly( name:'nightly1')
night.id=1
night.save(validate: false)
assert Nightly.get(1).name == 'nightly1'
assert Result.count() == 0
new Result(status: Constants.SUCCESS, type: Constants.INTEGRATION,
nightly: Nightly.get(1)).save(validate: false)
assert Result.count() == 1
assert Result.findByStatus(Constants.SUCCESS) != null // yay dynamic finders!
}
}
http://grails.org/doc/latest/guide/testing.html#unitTestingDomains
You'll have to update all your tests to the new ways, but it's much nicer that the old 1.3 ways.
So here is what we found.
With 1.3, you could do:
{
mockDomain(Nightly, [new Nightly(id: 7)])
mockDomain(Result, [
new Result(status: Constants.SUCCESS,
type: Constants.INTEGRATION, nightly: Nightly.get(7))
])
service.callSomething(results, Nightly.get(7))
assert result==Nightly.get(7).property
And it would work just fine. You whoul have a Mock domain object with and id of 7, and the get would work just fine.
Since then, something changed, and you can no longer set the id has part of the create.
What you need to do is this:
night = new Nightly( name:'nightly1')
night.id=1
mockDomain(Nightly, [night])
mockDomain(Result, [
new Result(status: Constants.SUCCESS, type: Constants.INTEGRATION, nightly: Nightly.get(1))
])
and that mostly sets up the mocks correctly.
The issue we ran into next was that outside of the mockDomain call, Nightly.get() would not work.
So now we need to save the "mocked" domains in local variables in order to do post action comparison and checks.
Not a completely horrible solution, but less elegant than we were hoping for.

More informative asserts in Scala

I'm looking for asserts in the style of Google's testing framework, where something like ASSERT_LT(a, b) will check that $a is less than $b, and if not, will print the values of $a and $b in the error message. The asserts I've found so far just stop execution without printing anything useful by default. Any pointers?
Bonus: I like to write assertion-heavy code (not just in the tests), so it would be nice if the assertions are quick to evaluate.
Context: I came across this when writing unittests using this code as a template.
Specs2 defines a number of matchers such as must be_<=. At first I thought these needed to integrated into a specification, but at the bottom of the matchers page it says that they are modular functionality that "you should be able to reuse in your own test
framework". As an example, "You can reuse [traits such as] org.specs2.matcher.MustMatchers to write anything like 1 must be_==(1) and
get a Result back".
ScalaTest has its own matchers, as well. For example, one must be < (7).
def assert_<[A <% Ordered[A]](a: => A, b: => A) {
assert(a < b, "Assertion failed: expected %s < %s" format (a, b))
}
scala> assert_<(2, 1)
java.lang.AssertionError: assertion failed: Assertion failed: expected 2 < 1