Permalink
Browse files

1 + 2 works

  • Loading branch information...
1 parent 5313400 commit 687492c96dd9f007bc1b0aa7e85ac6e1290e014b @kaiwren committed Oct 6, 2009
View
@@ -1,4 +1,4 @@
Nul
====
-Null object mojo for Rails. Finders, relationships, blah.
+Wha?
View
@@ -1,23 +0,0 @@
-gem 'rspec'
-require 'rake'
-require 'rake/rdoctask'
-require 'spec/rake/spectask'
-require 'spec'
-
-desc 'Default: run spec tests.'
-task :default => :spec
-
-desc "Run all specs"
-Spec::Rake::SpecTask.new(:spec) do |task|
- task.spec_files = FileList['spec/**/*_spec.rb']
- task.spec_opts = ['--options', 'spec/spec.opts']
-end
-
-desc 'Generate documentation for the nul plugin.'
-Rake::RDocTask.new(:rdoc) do |rdoc|
- rdoc.rdoc_dir = 'doc'
- rdoc.title = 'Nul'
- rdoc.options << '--line-numbers' << '--inline-source'
- rdoc.rdoc_files.include('README')
- rdoc.rdoc_files.include('lib/**/*.rb')
-end
Binary file not shown.
View
@@ -0,0 +1,23 @@
+gem 'rspec'
+require 'rake'
+require 'rake/rdoctask'
+require 'spec/rake/spectask'
+require 'spec'
+
+desc 'Default: run spec tests.'
+task :default => :spec
+
+desc "Run all specs"
+Spec::Rake::SpecTask.new(:spec) do |task|
+ task.spec_files = FileList['spec/**/*_spec.rb']
+ task.spec_opts = ['--options', 'spec/spec.opts']
+end
+
+desc 'Generate documentation for the nul plugin.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'doc'
+ rdoc.title = 'Nul'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
@@ -1,12 +0,0 @@
-sqlite:
- adapter: sqlite
- dbfile: nul_plugin_test.sqlite.db
-sqlite3:
- adapter: sqlite3
- dbfile: nul_plugin_test.sqlite3.db
-mysql:
- adapter: mysql
- host: localhost
- username:
- password:
- database: nul_plugin_test
No changes.
View
@@ -1,6 +0,0 @@
---colour
---format
-progress
---loadby
-mtime
---reverse
View
@@ -1,23 +0,0 @@
-$:.unshift(File.dirname(__FILE__) + '/../lib')
-
-require 'rubygems'
-require 'spec'
-require 'active_record'
-
-require File.dirname(__FILE__) + '/../init.rb'
-
-require 'active_record/fixtures'
-
-config = YAML::load(IO.read(File.dirname(__FILE__) + '/config/database.yml'))
-ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
-ActiveRecord::Base.establish_connection(config[ENV['DB'] || 'mysql'])
-
-load(File.dirname(__FILE__) + "/config/schema.rb")
-
-fixture_path = $LOAD_PATH.unshift(File.join(File.dirname(__FILE__),"fixtures"))
-
-# load model
-require File.join(File.dirname(__FILE__), 'models/ooga')
-
-Spec::Runner.configure do |config|
-end
@@ -0,0 +1,5 @@
+package com.overcast.nul.core;
+
+public interface NulBlock {
+ public NulObject invoke(NulObject context, NulObject data);
+}
@@ -0,0 +1,19 @@
+package com.overcast.nul.core;
+
+public class NulInteger extends NulObject {
+ private int value;
+
+ public NulInteger(String token) {
+ super(token);
+ value = Integer.parseInt(token);
+ defineReceiver("+", new NulBlock(){
+ public NulObject invoke(NulObject context, NulObject data) {
+ return new NulInteger(Integer.toString(((NulInteger)context).value + ((NulInteger)data).value));
+ }
+ });
+ }
+
+ public NulObject evaluate() {
+ return receivers.get(next.getToken()).invoke(this, next.nextObject());
+ }
+}
@@ -0,0 +1,34 @@
+package com.overcast.nul.core;
+
+import java.util.Map;
+import java.util.HashMap;
+
+public class NulObject {
+ protected String token = null;
+ protected NulObject next;
+ protected Map<String, NulBlock> receivers = new HashMap<String, NulBlock>();
+
+ public NulObject nextObject() {
+ return next;
+ }
+
+ public String getToken() {
+ return token;
+ }
+
+ public NulObject(String token) {
+ this.token = token;
+ }
+
+ public NulObject send(NulObject nulObject) {
+ return next = nulObject;
+ }
+
+ public NulObject evaluate() {
+ return next.evaluate();
+ }
+
+ public void defineReceiver(String signature, NulBlock block){
+ receivers.put(signature, block);
+ }
+}
@@ -0,0 +1,48 @@
+package com.overcast.nul.parser;
+
+import com.overcast.nul.core.NulObject;
+import com.overcast.nul.core.NulInteger;
+
+import java.io.Reader;
+import java.io.IOException;
+import java.util.regex.Pattern;
+
+public class NulParser {
+ public final static Pattern REGEX_NUMBER = Pattern.compile("[0-9]+");
+ public static final Pattern REGEX_WHITESPACES = Pattern.compile("\\s+");;
+ private NulObject context = new NulObject("");
+
+ public NulParser() {
+ }
+
+ public NulObject parse(Reader reader) throws IOException {
+ StringBuffer tokenBuffer = new StringBuffer();
+ String token = null;
+ boolean done = false;
+ NulObject current = context;
+ while (!done) {
+ int ch = reader.read();
+
+ switch (ch) {
+ case -1:
+ done = true;
+ case ' ':
+ token = tokenBuffer.toString();
+ tokenBuffer = new StringBuffer();
+ NulObject object = null;
+ if(REGEX_NUMBER.matcher(token).matches()) {
+ object = new NulInteger(token);
+ }
+ else{
+ object = new NulObject(token);
+ }
+ current = current.send(object);
+ break;
+ default:
+ char character = (char) ch;
+ tokenBuffer.append(character);
+ }
+ }
+ return context;
+ }
+}
@@ -0,0 +1,17 @@
+package com.overcast.nul.core;
+
+import org.junit.Test;
+import static org.hamcrest.Matchers.*;
+import static org.hamcrest.MatcherAssert.*;
+import com.overcast.nul.parser.NulParser;
+
+import java.io.StringReader;
+import java.io.IOException;
+
+public class NulIntegerTest {
+ @Test
+ public void shouldBeAbleToEvaluateASimpleExpression() throws IOException {
+ NulObject context = new NulParser().parse(new StringReader("1 + 2"));
+ assertThat(context.evaluate().getToken(), is(equalTo("3")));
+ }
+}
@@ -0,0 +1,11 @@
+package com.overcast.nul.core;
+
+import org.junit.Test;
+import static org.hamcrest.Matchers.*;
+import static org.hamcrest.MatcherAssert.*;
+
+public class NulObjectTest {
+ @Test
+ public void shouldKnowHowToAddRecieversToItself(){
+ }
+}
@@ -0,0 +1,42 @@
+package com.overcast.nul.parser;
+
+import org.junit.Test;
+import static org.hamcrest.Matchers.*;
+import static org.hamcrest.MatcherAssert.*;
+import com.overcast.nul.core.NulObject;
+
+import java.io.StringReader;
+import java.io.IOException;
+
+public class NulParserTest {
+ @Test
+ public void shouldBuildAnObjectChainFromASimpleExpression() throws IOException {
+ NulObject context = new NulParser().parse(new StringReader("1 + 2"));
+ NulObject tokenOne = context.nextObject();
+ NulObject tokenTwo = tokenOne.nextObject();
+ NulObject tokenThree = tokenTwo.nextObject();
+ assertThat(tokenOne.getToken(), is(equalTo("1")));
+ assertThat(tokenTwo.getToken(), is(equalTo("+")));
+ assertThat(tokenThree.getToken(), is(equalTo("2")));
+ }
+
+ @Test
+ public void regexpNumberMatchesNumbers(){
+ assertThat(NulParser.REGEX_NUMBER.matcher("0").matches(), is(equalTo(true)));
+ assertThat(NulParser.REGEX_NUMBER.matcher("123").matches(), is(equalTo(true)));
+
+ assertThat(NulParser.REGEX_NUMBER.matcher("a123").matches(), is(equalTo(false)));
+ assertThat(NulParser.REGEX_NUMBER.matcher("abcd").matches(), is(equalTo(false)));
+ }
+
+ @Test
+ public void regexpNumberMatchesWhitespaces(){
+ assertThat(NulParser.REGEX_WHITESPACES.matcher(" ").matches(), is(equalTo(true)));
+ assertThat(NulParser.REGEX_WHITESPACES.matcher(" ").matches(), is(equalTo(true)));
+ assertThat(NulParser.REGEX_WHITESPACES.matcher(" ").matches(), is(equalTo(true)));
+
+ assertThat(NulParser.REGEX_WHITESPACES.matcher(" a123").matches(), is(equalTo(false)));
+ assertThat(NulParser.REGEX_WHITESPACES.matcher("a ").matches(), is(equalTo(false)));
+ assertThat(NulParser.REGEX_WHITESPACES.matcher("a a").matches(), is(equalTo(false)));
+ }
+}

0 comments on commit 687492c

Please sign in to comment.