Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added spock tests, codenarc #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ out/*
target/*
.idea/*
*.iml
/.settings/
158 changes: 158 additions & 0 deletions CodeNarcReport.html

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions grails-app/conf/BuildConfig.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,15 @@ grails.project.dependency.resolution = {
}
dependencies {
}
plugins{
test ":spock:0.5-groovy-1.7"
build ":codenarc:0.16.1"
}

codenarc.reports = {
StockAnalysis('html') {
outputFile = 'CodeNarc-Report.html'
title = 'Stock installation CodeNarc Report'
}
}
}
692 changes: 692 additions & 0 deletions stacktrace.log

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions test/integration/JQueryServiceIntegrationSpec.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import grails.plugin.spock.*

class JQueryServiceIntegrationSpec extends IntegrationSpec {
JQueryService jQueryService

def "verify that paths that exist are successfully found"(){
when: "we try to see if a path exists"
def test = jQueryService.exist("grails-app", "domain")
then: "it is successfully found"
test == true
}

def "verify that paths that don't exist are successfully identified"(){
when: "we try to see if a path exists"
def test = jQueryService.exist("foo", "bar")
then: "it is not found"
test == false
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.codehaus.groovy.grails.plugins.jquery;

import grails.plugin.spock.*
import org.codehaus.groovy.grails.commons.ApplicationHolder

class JQueryConfigSpec extends IntegrationSpec {
def "verify our current version is the latest release, 1 7 1"(){
expect: "our current version is the latest release. Update this test when you update jQuery!"
new JQueryConfig().SHIPPED_VERSION == "1.7.1"
}
def "verify that we can initialize the config with default plugins"(){
when: "we try to load a config file and build a JQueryConfig"
// doesn't work. Can't set metadata
ApplicationHolder.application.config.jquery.defaultPlugins = ["jqueryui", "cycle"]
def test = new JQueryConfig()
test.init()
then: "we should have the plugin info available"
test.defaultPlugins == ["jqueryui", "cycle"]
}

def "verify that we can initialize the config with application metadata"(){
// I have no idea how we can mock the ApplicationHolder the class will use
// or dynamically set the metadata (we could insert fake properties in a test, but
// it doesn't seem that metadata has setters as well as getters
when: "we try to load an application's metadata and build a JQueryConfig"
// doesn't work. Can't set metadata
ApplicationHolder.application.metadata +=["jquery.plugins.foo":"baz"]
def test = new JQueryConfig()
test.init()
then: "we should have the plugin info available"
test.plugins.foo == ["baz"]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.codehaus.groovy.grails.plugins.jquery;

import grails.plugin.spock.*

class JQueryProviderIntegrationSpec extends IntegrationSpec {
def "verify that ajax forms are prepared successfully for submit tags"(){
given: "a set of attributes, including forSubmitTag"
def attrs = ["blah" :"boo", "forSubmitTag" : "true"]
when: "an ajax form is prepared"
def test = new JQueryProvider().prepareAjaxForm(attrs)
then: "the resultant jQuery should serialize the surrounding form"
test == "jQuery(this).parents('form:first').serialize()".toString()
attrs.params == "jQuery(this).parents('form:first').serialize()".toString()
}

def "verify that ajax forms are prepared successfully"(){
given: "a set of attributes"
def attrs = [blah :"boo"]
when: "an ajax form is prepared"
def test = new JQueryProvider().prepareAjaxForm(attrs)
then: "the resultant jQuery should serialize the form"
test == "jQuery(this).serialize()".toString()
attrs.params == "jQuery(this).serialize()".toString()
}

// the buildCallback() helper method should probably be private
// I have no idea what taglib to pass, as line 186 of
// groovy/org/codehaus/groovy/grails/plugins/web/taglib/JavascriptTagLib.groovy
// references an "owner" that is apparently referenced nowhere else in the document
def "verify that remote function works in happy case -- INCOMPLETE"(){
when: "we try to execute a remote function"
def test = new JQueryProvider().doRemoteFunction()
then: "the resultant jQuery should look like standard AJAX"
test == ""
}
}
39 changes: 39 additions & 0 deletions test/unit/JQueryResourceTagLibSpec.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import grails.plugin.spock.*

class JQueryResourceTagLibSpec extends TagLibSpec {
// this is apparently an undocumented feature on http://grails.org/plugin/jquery. Is it necessary? Removable?
def "verify that when the resources tag is called with local attribute, script comes from app"(){
when: "I try to load resources"
def result = resources( local: 'true' )
then: "jQuery is loaded from local app"
result == "<script src='js/jquery/jquery-1.7.1.min.js' type='text/javascript'></script>"
}

def "verify that when the resources tag is called without local attribute, script comes from plugin"(){
when: "I try to load resources"
def result = resources()
then: "jQuery is loaded from the plugin"
result == "<script src='jquery/js/jquery/jquery-1.7.1.min.js' type='text/javascript'></script>"
}

def "verify that when the resources tag is called with a non boolean local attribute, script comes from plugin"(){
when: "I try to load resources"
def result = resources( local: 'gaz' )
then: "jQuery is loaded from the plugin"
result == "<script src='jquery/js/jquery/jquery-1.7.1.min.js' type='text/javascript'></script>"
}

// Is this really testable? should this be refactored?
def "verify that when the resources tag is called in the development environment, min version is not loaded"(){
when: "I try to load resources in development environment"
def result = resources( local: 'gaz' )
then: "jQuery is loaded from the plugin"
result == "<script src='jquery/js/jquery/jquery-1.7.1.min.js' type='text/javascript'></script>"
}

// RESOURCE is really ugly and difficult to test...
// effects in the comment appears to be unused. mode is undocumented in the plugin docs
// it seems to try to incorporate much functionality that the resources plugin does.
// does it make sense to just have this plugin depend on that and remove these tags?
// also, the addResource should probably be private, as it looks like it is meant to be a utility method
}
15 changes: 0 additions & 15 deletions test/unit/JQueryResourceTagLibTests.groovy

This file was deleted.

33 changes: 33 additions & 0 deletions test/unit/JQueryServiceSpec.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import grails.plugin.spock.*

class JQueryServiceSpec extends UnitSpec {
// I think this is a _stateful_ service whose sole purpose in life is to provide
// properties to JQueryResource... :/
// it looks like exist() and cleanPath() are the only publicly accessible methods
// cleanPath() appears to mean appending a trailing slash if necessary. In which case, it should be renamed
// (used in jQueryResource resource()), the other helper methods should probably be private
// seems like there's lots of duplication of implementation here too

JQueryService toTest = new JQueryService()

def "verify that paths are correctly 'cleaned' by adding a trailing slash"(){
when: "we try to clean a path without a trailing slash"
def test = toTest.cleanPath("foo")
then: "the result contains a trailing slash"
test == "foo/"
}

def "verify that paths while already contain a trailing slash are unmodified"(){
when: "we try to clean a path containing a trailing slash"
def test = toTest.cleanPath("foo/")
then: "the result does not have another slash added to it"
test == "foo/"
}

def "verify that empty strings as paths are unmodified"(){
when: "we try to clean a path that's the empty string"
def test = toTest.cleanPath("")
then: "the result is the empty string"
test == ""
}
}
15 changes: 0 additions & 15 deletions test/unit/JQueryServiceTests.groovy

This file was deleted.

62 changes: 62 additions & 0 deletions test/unit/JQueryTagLibSpec.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import grails.plugin.spock.*

class JQueryTagLibSpec extends TagLibSpec {
def "verify that using the jQuery tag adds to the jQuery ready function"(){
when: "a piece of Javascript to be executed on ready is passed to jQuery"
def test = jquery(attrs: []) { "alert('Hello world');" }
then: "the code is wrapped in jQuery"
test == '<script type="text/javascript">jQuery(function(){alert(\'Hello world\');}); </script>'
}

def "verify that we can toggle an item using defaults"(){
when: "we pass toggle, giving the html id of the element, and the element to be toggled"
def test = toggle(sourceId:"foo", targetId:"bar")
then: "we get jQuery output that toggles the element"
test == /jQuery("#foo").click(function(){jQuery("#bar").toggle("normal"); return false; });/
}

// if toggleelement is deprecated, it should be removed.
// until then, since it appears to do the same thing with a different API,
// it should redirect to toggle internally with the correct parameters rather than duplicating implementation

def "verify that we can toggle an item with a custom event"(){
when: "we pass toggle, giving the html id of the element, the element to be toggled, and an event"
def test = toggle(sourceId:"foo", targetId:"bar", event:"hover")
then: "we get jQuery output that toggles the element on the event"
test == /jQuery("#foo").hover(function(){jQuery("#bar").toggle("normal"); return false; });/
}

def "verify that we can toggle an item with custom speed"(){
when: "we pass toggle, giving the html id of the element, the element to be toggled, and custom speed"
def test = toggle(sourceId:"foo", targetId:"bar", speed:"fast")
then: "we get jQuery output that toggles the element at the custom speed"
test == /jQuery("#foo").click(function(){jQuery("#bar").toggle("fast"); return false; });/
}

def "verify that we can toggle an item with custom speed and custom event"(){
when: "we pass toggle, giving the html id of the element, the element to be toggled, a custom event, and custom speed"
def test = toggle(sourceId:"foo", targetId:"bar", event:"hover", speed:"fast")
then: "we get jQuery output that toggles the element on the custom event at the custom speed"
test == /jQuery("#foo").hover(function(){jQuery("#bar").toggle("fast"); return false; });/
}

def "verify that we can get the field value for an attribute selector"(){
when: "we try to select for an attribute"
def test = fieldValue(selector:'input[name*="man"]')
then: "we get jQuery output that gets the field Value"
test == /jQuery('input[name*="man"]').fieldValue()[0]/
}

def "verify that we can get the field value for an element id"(){
when: "we try to select for an element"
def test = fieldValue(elementId:'foo')
then: "we get jQuery output that gets the field Value"
test == /jQuery('#foo').fieldValue()[0]/
}

// note that the body of the closure implies that if _both_ the attribute selector
// and an element id are specified, the closure chooses the selector silently and
// ignores the elementId. While such usage may seem stupid, it may be worth refactoring
// to alert the developer that it should be one OR the other, NOT both
// since this is just wrapping a jQuery call in javascript, I'm not sure how much value this adds
}
15 changes: 0 additions & 15 deletions test/unit/JQueryTagLibTests.groovy

This file was deleted.