/*-- $Copyright (c) 2014 Christoffer Fink$

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 net.finkn.inputspec.v050;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

import net.finkn.inputspec.tools.ParamCfg;
import net.finkn.inputspec.tools.DesignSpaceCfg;

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

import se.miun.itm.input.model.InPUTException;
import se.miun.itm.input.model.design.IDesignSpace;

/**
 * Tests the behavior of {@code setFixed} of design space in InPUT4j 0.5.
 *
 * @author Christoffer Fink
 */
public class SetFixedTest {

  private final ParamCfg param = ParamCfg.builder()
    .inclMin(1).inclMax(3).build();
  private final String id = param.getId();

  private IDesignSpace space;

  @Before
  public void setup() throws Throwable {
    this.space = DesignSpaceCfg.getInstance(param).getDesignSpace();
  }

  // TODO: link to a test showing how this affects design creation.
  /**
   * Fixing a parameter to an out-of-range value is legal.
   * This is consistent with the behavior demonstrated in
   * {@link FixedNumericTest#fixedTakesPrecedenceOverLimits}.
   */
  @Test
  public void parametersCanBeFixedToOutOfRangeValues() throws Throwable {
    space.setFixed(id, "5"); // 5 ∉ [1,3]
    assertThat(space.next(id), is(equalTo(5)));
  }

  /** Float values are truncated when fixing an integer parameter. */
  @Test
  public void floatValuesAreTruncatedForIntegerParameter() throws Throwable {
    space.setFixed(id, "1.5");
    assertThat(space.next(id), is(equalTo(1)));
  }

  /**
   * Parameters are fixed only in the sense of specifying the values that should
   * be generated, not in the sense that they are immutable.
   * Parameters can thus be fixed multiple times, and even to new values.
   */
  @Test
  public void parametersCanBeFixedMultipleTimes() throws Throwable {
    space.setFixed(id, "1");
    space.setFixed(id, "1");
    space.setFixed(id, "2");
    assertThat(space.next(id), is(equalTo(2)));
  }

  /** Setting a fixed parameter to a new value is illegal. */
  @Test(expected = InPUTException.class)
  public void setValueOnFixedParamIsIllegalIfValuesDiffer() throws Throwable {
    space.setFixed(id, "1");
    space.nextDesign("Design").setValue(id, 2);
  }

  /** Setting a fixed parameter to the same value is legal. */
  @Test
  public void setValueOnFixedParamIsLegalIfValuesMatch() throws Throwable {
    space.setFixed(id, "1");
    space.nextDesign("Design").setValue(id, 1);
  }
}

@@ -1233,16 +1233,66 @@
 ertThat;
+%0Aimport static net.finkn.inputspec.tools.Helper.*;
 %0A%0Aimport
@@ -1706,52 +1706,29 @@
 m = 
-ParamCfg.builder()%0A    .inclMin(1).inclMax(3
+pb().interval(%22%5B1,3%5D%22
 ).bu
@@ -1881,57 +1881,19 @@
 e = 
-DesignSpaceCfg.getInstance(param).getDesignSpace(
+space(param
 );%0A 
@@ -3449,32 +3449,32 @@
 Fixed(id, %221%22);%0A
-
     space.nextDe
@@ -3508,11 +3508,1007 @@
  1);%0A  %7D
+%0A%0A  /**%0A   * Fixing a parameter to %7B@code null%7D makes the parameter unfixed.%0A   * In contrast to %7B@link #setFixedToNullUnfixesParameter%7D, this test%0A   * unfixes a parameter that was originally unfixed in the configuration, but%0A   * was subsequently fixed by %7B@code setFixed%7D.%0A   */%0A  @Test%0A  public void setFixedToNullCancelsPreviousFix() throws Throwable %7B%0A    space.setFixed(id, %221%22);%0A    space.setFixed(id, null);%0A    space.nextDesign(%22Design%22).setValue(id, 2);%0A  %7D%0A%0A  /**%0A   * Fixing a parameter to %7B@code null%7D makes the parameter unfixed.%0A   * In contrast to %7B@link #setFixedToNullCanclesPreviousFix%7D, this test%0A   * unfixes a parameter that was defined as fixed in the configuration.%0A   * The effect is the same in both cases.%0A   */%0A  @Test%0A  public void setFixedToNullUnfixesParameter() throws Throwable %7B%0A    ParamCfg fixed = pb().fixed(1).build();%0A    String id = fixed.getId();%0A    space = space(fixed);%0A    space.setFixed(id, null);%0A    space.nextDesign(%22Design%22).setValue(id, 2);%0A  %7D
 %0A%7D%0A

add more tests