Class: CIAT::Compilers::Java

Inherits:
Object
  • Object
show all
Includes:
Differs::HtmlDiffer
Defined in:
lib/ciat/compilers/java.rb

Overview

Implements a processor written in Java.

It requires source and compilation elements.

  • source is used as source to the Java compiler.

  • compilation is used for comparsion.

Best Practices

Suppose you use Eclipse to develop your compiler or interpreter, and you have this folder structure:

  • bin stores your compiled classes (under test)

  • lib contains support JAR files

  • acceptance is a root folder for your CIAT tests with your Rakefile

You may find this classpath useful:

Dir.glob('../lib/*.jar').join(':') + ":../bin"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Differs::HtmlDiffer

#diff_options, #html_diff

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:

  • description specifies 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, options={})
  @classpath = classpath
  @compiler_class = compiler_class
  @descriptions = {}
  @description = options[:description] || "compiler (implemented in Java)"
  @light = options[:light] || TrafficLight.new
end

Instance Attribute Details

#lightObject

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

#describeObject

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_testObject

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