Class: CIAT::Compilers::Java
- Inherits:
-
Object
- Object
- CIAT::Compilers::Java
- Includes:
- Differs::HtmlDiffer
- Defined in:
- lib/ciat/compilers/java.rb
Overview
Implements a processor written in Java.
It requires source and compilation elements.
-
sourceis used as source to the Java compiler. -
compilationis used for comparsion.
Best Practices
Suppose you use Eclipse to develop your compiler or interpreter, and you have this folder structure:
-
binstores your compiled classes (under test) -
libcontains support JAR files -
acceptanceis a root folder for your CIAT tests with yourRakefile
You may find this classpath useful:
Dir.glob('../lib/*.jar').join(':') + ":../bin"
Instance Attribute Summary collapse
-
#light ⇒ Object
The traffic light to indicate the success or failure of the processor.
Instance Method Summary collapse
-
#compile(crate) ⇒ Object
Does the actual compilation.
-
#describe ⇒ Object
Return a description of the processor.
-
#diff(crate) ⇒ Object
Computes the difference between the generated and expected output.
-
#elements(crate) ⇒ Object
The interesting elements of a Java processor based on the traffic light’s setting.
-
#for_test ⇒ Object
Produces a clone for an individual test.
-
#initialize(classpath, compiler_class, options = {}) ⇒ Java
constructor
Constructs a “Java compiler” object.
-
#process(crate) ⇒ Object
Compiles the code and diffs the output.
Methods included from Differs::HtmlDiffer
Constructor Details
#initialize(classpath, compiler_class, options = {}) ⇒ Java
Constructs a “Java compiler” object. classpath is the complete classpath to execute the compiler. compiler_class is the fully qualified name of the class that executes your compiler; this driver should take two command-line arguments: the name of the source file and the name of the generated target-code file.
Possible options:
-
descriptionspecifies a descriptive name for your compiler; used in the HTML report.
33 34 35 36 37 38 39 |
# File 'lib/ciat/compilers/java.rb', line 33 def initialize(classpath, compiler_class, ={}) @classpath = classpath @compiler_class = compiler_class @descriptions = {} @description = [:description] || "compiler (implemented in Java)" @light = [:light] || TrafficLight.new end |
Instance Attribute Details
#light ⇒ Object
The traffic light to indicate the success or failure of the processor.
22 23 24 |
# File 'lib/ciat/compilers/java.rb', line 22 def light @light end |
Instance Method Details
#compile(crate) ⇒ Object
Does the actual compilation.
69 70 71 |
# File 'lib/ciat/compilers/java.rb', line 69 def compile(crate) system "java -cp '#{@classpath}' #{@compiler_class} '#{crate.element(:source).as_file}' '#{crate.element(:compilation, :generated).as_file}' 2> '#{crate.element(:compilation, :error).as_file}'" end |
#describe ⇒ Object
Return a description of the processor.
49 50 51 |
# File 'lib/ciat/compilers/java.rb', line 49 def describe @description end |
#diff(crate) ⇒ Object
Computes the difference between the generated and expected output.
74 75 76 77 78 79 |
# File 'lib/ciat/compilers/java.rb', line 74 def diff(crate) html_diff( crate.element(:compilation).as_file, crate.element(:compilation, :generated).as_file, crate.element(:compilation, :diff).as_file) end |
#elements(crate) ⇒ Object
The interesting elements of a Java processor based on the traffic light’s setting.
83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/ciat/compilers/java.rb', line 83 def elements(crate) case light.setting when :green crate.elements(:source, :compilation) when :yellow crate.elements(:source, :compilation_error) when :red crate.elements(:source, :compilation_diff) else raise "unexpected setting #{light.setting}" end end |
#for_test ⇒ Object
Produces a clone for an individual test.
42 43 44 45 46 |
# File 'lib/ciat/compilers/java.rb', line 42 def for_test copy = clone copy.light = light.clone copy end |
#process(crate) ⇒ Object
Compiles the code and diffs the output.
54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/ciat/compilers/java.rb', line 54 def process(crate) # TODO: verify required elements if compile(crate) if diff(crate) light.green! else light.red! end else light.yellow! end crate end |