View Javadoc
1   /*
2    * Copyright 2007, 2008, 2009 Ange Optimization ApS
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package eu.simuline.octave.io.impl;
17  
18  import eu.simuline.octave.OctaveEngine;
19  import eu.simuline.octave.OctaveEngineFactory;
20  import eu.simuline.octave.exception.OctaveParseException;
21  import eu.simuline.octave.exception.OctaveRecoverableException;
22  import eu.simuline.octave.type.OctaveString;
23  
24  import static org.junit.Assert.assertEquals;
25  import static org.junit.Assert.assertTrue;
26  import static org.junit.Assert.fail;
27  
28  import org.junit.Ignore;
29  import org.junit.Test;
30  
31  /**
32   * Test reading of sq_string (can not be written)
33   */
34  public class TestIoOctaveSqString {
35  
36      /**
37       * Test that string read from octave is what we expect
38       */
39      @Test public void testOctaveRead() {
40          final OctaveEngine octave = new OctaveEngineFactory().getScriptEngine();
41          read(octave, "sample text", "sample text");
42          read(octave, "", "");
43          try { // not implemented yet, but does not break the engine
44              read(octave, "a\\nb", "a\nb");
45              fail();
46          } catch (final OctaveParseException e) {
47              assertEquals("Handling of escape char (\\) not done, line='a\\nb'",
48  			 e.getMessage());
49              assertTrue(OctaveRecoverableException.class.isInstance(e));
50          }
51          // read(octave, "a\\tb", "a\tb"); same as \n example
52          octave.close();
53      }
54  
55      private static void read(final OctaveEngine octave, 
56  			     final String input, 
57  			     final String expected) {
58          final String key = "octave_string";
59          octave.eval(key + " = '" + input + "';");
60          final OctaveString output = octave.get(OctaveString.class, key);
61          assertEquals(expected, output.getString());
62      }
63  
64  }