/**
 * The MIT License (MIT)
 * 
 * Copyright (c) 2014 mcflugen
 * 
 * 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.
 */
package edu.colorado.csdms.wmt.client;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.google.gwt.junit.client.GWTTestCase;

import edu.colorado.csdms.wmt.client.data.ValueJSO;

/**
 * Tests for {@link ValueJSO} of type "float". JUnit integration is provided by
 * extending {@link GWTTestCase}.
 * 
 * @see http://www.gwtproject.org/doc/latest/DevGuideTesting.html
 * @see http://www.gwtproject.org/doc/latest/tutorial/JUnit.html
 * @author Mark Piper (mark.piper@colorado.edu)
 */
public class ValueJSOFloatTest extends GWTTestCase {

  private ValueJSO jso;
  private String type = "float";
  private float defaultValue;
  private float max;
  private float min;
  private String units;

  /**
   * The module that sources this class. Must be present.
   */
  @Override
  public String getModuleName() {
     return "edu.colorado.csdms.wmt.WMT";
  }

  /**
   * A JSNI method that defines a fixture for the tests. Returns a
   * {@link ValueJSO} object for testing.
   * 
   * @param defaultValue
   * @param units
   * @param max
   * @param min
   * @param type
   */
  private native ValueJSO testValueJSO(float defaultValue, String units,
      float max, float min, String type) /*-{
		return {
			"default" : defaultValue,
			"units" : units,
			"range" : {
				"max" : max,
				"min" : min
			},
			"type" : type
		};
  }-*/;
  
  @Before
  @Override
  protected void gwtSetUp() throws Exception {
    defaultValue = (float) 100.0;
    units = "m";
    max = (float) 10000.0;
    min = (float) 0.0;
    jso = testValueJSO(defaultValue, units, max, min, type);
  }

  @After
  @Override
  protected void gwtTearDown() throws Exception {
  }

  /*
   * Test getting type.
   */
  @Test
  public void testGetType() {
    assertEquals(type, jso.getType());
  }

  /*
   * Test getting default.
   */
  @Test
  public void testGetDefault() {
    float theDefault = Float.valueOf(jso.getDefault());
    assertEquals(defaultValue, theDefault);
  }

  /*
   * Test setting default.
   */
  @Test
  public void testSetDefault() {
    float newDefault = (float) 42.0;
    String newDefaultString = Float.toString(newDefault);
    jso.setDefault(newDefault);
    assertEquals(newDefaultString, jso.getDefault());
  }

  /*
   * Test getting units.
   */
  @Test
  public void testGetUnits() {
    assertEquals(units, jso.getUnits());
  }
  
  /*
   * Test getting min.
   */
  @Test
  public void testGetMin() {
    float theMin = Float.valueOf(jso.getMin());
    assertEquals(min, theMin);
  }

  /*
   * Test getting max.
   */
  @Test
  public void testGetMax() {
    float theMax = Float.valueOf(jso.getMax());
    assertEquals(max, theMax);
  }
  
  /*
   * Test getting choices. Should return null if not present.
   */
  @Test
  public void testGetChoices() {
    assertNull(jso.getChoices());
  }
  
  /*
   * Test getting files. Should return null if not present.
   */
  @Test
  public void testGetFiles() {
    assertNull(jso.getFiles());
  }
}

@@ -3150,32 +3150,410 @@
  setting default
+ as a float value. Compare result as a float with a %0A   * tolerance of 1e-3, since this is the number of digits kept in the WMT%0A   * client.%0A   * %0A   * I tried to test the effects of floating point precision by using a number %0A   * near the value of the tolerance and a number that has more significant%0A   * digits than a float can hold. There's likely a better way to test this
 .%0A   */%0A  @Test%0A
@@ -3577,32 +3577,74 @@
 tSetDefault() %7B%0A
+    float tolerance = (float) 0.001;%0A    %0A
     float newDef
@@ -3647,16 +3647,17 @@
 wDefault
+0
  = (floa
@@ -3666,68 +3666,380 @@
  42.
-0;%0A    String newDefaultString = F
+3; // arbitrary%0A    jso.setDefault(newDefault0);%0A    assertEquals(newDefault0, Float.valueOf(jso.getDefault()), tolerance);%0A%0A    float newDefault1 = (f
 loat
-.toString(newDefault);
+) 0.001; // arbitrary, near tolerance%0A    jso.setDefault(newDefault1);%0A    assertEquals(newDefault1, Float.valueOf(jso.getDefault()), tolerance);%0A%0A    float newDefault2 = (float) 12345678.9; // arbitrary, lotsa digits
 %0A   
@@ -4060,24 +4060,25 @@
 t(newDefault
+2
 );%0A    asser
@@ -4099,16 +4099,25 @@
 ault
-String, 
+2, Float.valueOf(
 jso.
@@ -4125,24 +4125,36 @@
 etDefault())
+, tolerance)
 ;%0A  %7D%0A%0A  /*%0A

Use a tolerance in ValueJSOFloatTest