Преглед изворни кода

More basic stuff + tests

Implement String>>streamContents:, String>>join:, JQuery>>attrAt:put:
Laurent Laffont пре 12 година
родитељ
комит
ddca2dc672
13 измењених фајлова са 270 додато и 0 уклоњено
  1. 15 0
      js/JQuery.deploy.js
  2. 15 0
      js/JQuery.js
  3. 33 0
      js/Kernel-Tests.deploy.js
  4. 33 0
      js/Kernel-Tests.js
  5. 48 0
      js/Kernel.deploy.js
  6. 48 0
      js/Kernel.js
  7. 15 0
      js/SUnit.deploy.js
  8. 15 0
      js/SUnit.js
  9. 1 0
      js/jtalk.js
  10. 5 0
      st/JQuery.st
  11. 18 0
      st/Kernel-Tests.st
  12. 20 0
      st/Kernel.st
  13. 4 0
      st/SUnit.st

+ 15 - 0
js/JQuery.deploy.js

@@ -645,6 +645,21 @@ referencedClasses: []
 }),
 smalltalk.JQuery);
 
+smalltalk.addMethod(
+'_attrAt_put_',
+smalltalk.method({
+selector: 'attrAt:put:',
+category: 'attributes',
+fn: function (aString, anotherString){
+var self=this;
+self['@jquery'].attr(aString, anotherString);
+return self;},
+source: unescape('attrAt%3A%20aString%20put%3A%20anotherString%0A%20%20%20%20%22Set%20the%20value%20of%20an%20attribute%20for%20the%20first%20element%20in%20the%20set%20of%20matched%20elements.%22%0A%20%20%20%20%3Cself%5B%27@jquery%27%5D.attr%28aString%2C%20anotherString%29%3E'),
+messageSends: [],
+referencedClasses: []
+}),
+smalltalk.JQuery);
+
 
 smalltalk.addMethod(
 '_fromString_',

+ 15 - 0
js/JQuery.js

@@ -645,6 +645,21 @@ referencedClasses: []
 }),
 smalltalk.JQuery);
 
+smalltalk.addMethod(
+'_attrAt_put_',
+smalltalk.method({
+selector: 'attrAt:put:',
+category: 'attributes',
+fn: function (aString, anotherString){
+var self=this;
+self['@jquery'].attr(aString, anotherString);
+return self;},
+source: unescape('attrAt%3A%20aString%20put%3A%20anotherString%0A%20%20%20%20%22Set%20the%20value%20of%20an%20attribute%20for%20the%20first%20element%20in%20the%20set%20of%20matched%20elements.%22%0A%20%20%20%20%3Cself%5B%27@jquery%27%5D.attr%28aString%2C%20anotherString%29%3E'),
+messageSends: [],
+referencedClasses: []
+}),
+smalltalk.JQuery);
+
 
 smalltalk.addMethod(
 '_fromString_',

+ 33 - 0
js/Kernel-Tests.deploy.js

@@ -0,0 +1,33 @@
+smalltalk.addClass('StringTest', smalltalk.TestCase, [], 'Kernel-Tests');
+smalltalk.addMethod(
+'_testJoin',
+smalltalk.method({
+selector: 'testJoin',
+category: 'tests',
+fn: function (){
+var self=this;
+smalltalk.send(self, "_assert_equals_", [unescape("hello%2Cworld"), smalltalk.send(unescape("%2C"), "_join_", [["hello", "world"]])]);
+return self;},
+source: unescape('testJoin%0A%09self%20assert%3A%20%27hello%2Cworld%27%20equals%3A%20%28%27%2C%27%20join%3A%20%23%28%27hello%27%20%27world%27%29%29'),
+messageSends: ["assert:equals:", "join:"],
+referencedClasses: []
+}),
+smalltalk.StringTest);
+
+smalltalk.addMethod(
+'_testStreamContents',
+smalltalk.method({
+selector: 'testStreamContents',
+category: 'tests',
+fn: function (){
+var self=this;
+smalltalk.send(self, "_assert_equals_", ["hello world", smalltalk.send(smalltalk.String, "_streamContents_", [(function(aStream){return (function($rec){smalltalk.send($rec, "_nextPutAll_", ["hello"]);smalltalk.send($rec, "_space", []);return smalltalk.send($rec, "_nextPutAll_", ["world"]);})(aStream);})])]);
+return self;},
+source: unescape('testStreamContents%0A%09self%20%0A%09%09assert%3A%20%27hello%20world%27%20%0A%09%09equals%3A%20%28String%20streamContents%3A%20%5B%3AaStream%7C%20aStream%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%09%09%09%09%09nextPutAll%3A%20%27hello%27%3B%20space%3B%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%09%09%09%09%09nextPutAll%3A%20%27world%27%5D%29%20'),
+messageSends: ["assert:equals:", "streamContents:", "nextPutAll:", "space"],
+referencedClasses: [smalltalk.String]
+}),
+smalltalk.StringTest);
+
+
+

+ 33 - 0
js/Kernel-Tests.js

@@ -0,0 +1,33 @@
+smalltalk.addClass('StringTest', smalltalk.TestCase, [], 'Kernel-Tests');
+smalltalk.addMethod(
+'_testJoin',
+smalltalk.method({
+selector: 'testJoin',
+category: 'tests',
+fn: function (){
+var self=this;
+smalltalk.send(self, "_assert_equals_", [unescape("hello%2Cworld"), smalltalk.send(unescape("%2C"), "_join_", [["hello", "world"]])]);
+return self;},
+source: unescape('testJoin%0A%09self%20assert%3A%20%27hello%2Cworld%27%20equals%3A%20%28%27%2C%27%20join%3A%20%23%28%27hello%27%20%27world%27%29%29'),
+messageSends: ["assert:equals:", "join:"],
+referencedClasses: []
+}),
+smalltalk.StringTest);
+
+smalltalk.addMethod(
+'_testStreamContents',
+smalltalk.method({
+selector: 'testStreamContents',
+category: 'tests',
+fn: function (){
+var self=this;
+smalltalk.send(self, "_assert_equals_", ["hello world", smalltalk.send(smalltalk.String, "_streamContents_", [(function(aStream){return (function($rec){smalltalk.send($rec, "_nextPutAll_", ["hello"]);smalltalk.send($rec, "_space", []);return smalltalk.send($rec, "_nextPutAll_", ["world"]);})(aStream);})])]);
+return self;},
+source: unescape('testStreamContents%0A%09self%20%0A%09%09assert%3A%20%27hello%20world%27%20%0A%09%09equals%3A%20%28String%20streamContents%3A%20%5B%3AaStream%7C%20aStream%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%09%09%09%09%09nextPutAll%3A%20%27hello%27%3B%20space%3B%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%09%09%09%09%09nextPutAll%3A%20%27world%27%5D%29%20'),
+messageSends: ["assert:equals:", "streamContents:", "nextPutAll:", "space"],
+referencedClasses: [smalltalk.String]
+}),
+smalltalk.StringTest);
+
+
+

+ 48 - 0
js/Kernel.deploy.js

@@ -4450,6 +4450,21 @@ referencedClasses: []
 }),
 smalltalk.String);
 
+smalltalk.addMethod(
+'_join_',
+smalltalk.method({
+selector: 'join:',
+category: 'split join',
+fn: function (aCollection){
+var self=this;
+return smalltalk.send(smalltalk.String, "_streamContents_", [(function(stream){return smalltalk.send(aCollection, "_do_separatedBy_", [(function(each){return smalltalk.send(stream, "_nextPutAll_", [smalltalk.send(each, "_asString", [])]);}), (function(){return smalltalk.send(stream, "_nextPutAll_", [self]);})]);})]);
+return self;},
+source: unescape('join%3A%20aCollection%20%0A%09%5E%20String%0A%09%09streamContents%3A%20%5B%3Astream%20%7C%20aCollection%0A%09%09%09%09do%3A%20%5B%3Aeach%20%7C%20stream%20nextPutAll%3A%20each%20asString%5D%20%0A%09%09%09%09separatedBy%3A%20%5Bstream%20nextPutAll%3A%20self%5D%5D'),
+messageSends: ["streamContents:", "do:separatedBy:", "nextPutAll:", "asString"],
+referencedClasses: [smalltalk.String]
+}),
+smalltalk.String);
+
 
 smalltalk.addMethod(
 '_streamClass',
@@ -4556,6 +4571,24 @@ referencedClasses: []
 }),
 smalltalk.String.klass);
 
+smalltalk.addMethod(
+'_streamContents_',
+smalltalk.method({
+selector: 'streamContents:',
+category: 'instance creation',
+fn: function (blockWithArg){
+var self=this;
+var stream=nil;
+stream=smalltalk.send(smalltalk.send(self, "_streamClass", []), "_on_", [smalltalk.send(smalltalk.String, "_new", [])]);
+smalltalk.send(blockWithArg, "_value_", [stream]);
+return smalltalk.send(stream, "_contents", []);
+return self;},
+source: unescape('streamContents%3A%20blockWithArg%0A%09%7Cstream%7C%0A%09stream%20%3A%3D%20%28self%20streamClass%20on%3A%20String%20new%29.%0A%09blockWithArg%20value%3A%20stream.%0A%09%5E%20stream%20contents'),
+messageSends: ["on:", "streamClass", "new", "value:", "contents"],
+referencedClasses: [smalltalk.String]
+}),
+smalltalk.String.klass);
+
 
 smalltalk.addClass('Array', smalltalk.SequenceableCollection, [], 'Kernel');
 smalltalk.addMethod(
@@ -6253,6 +6286,21 @@ referencedClasses: [smalltalk.String]
 }),
 smalltalk.StringStream);
 
+smalltalk.addMethod(
+'_space',
+smalltalk.method({
+selector: 'space',
+category: 'writing',
+fn: function (){
+var self=this;
+smalltalk.send(self, "_nextPut_", [" "]);
+return self;},
+source: unescape('space%0A%09self%20nextPut%3A%20%27%20%27'),
+messageSends: ["nextPut:"],
+referencedClasses: []
+}),
+smalltalk.StringStream);
+
 
 
 smalltalk.addClass('ClassCommentReader', smalltalk.Object, ['class', 'chunkParser'], 'Kernel');

+ 48 - 0
js/Kernel.js

@@ -4450,6 +4450,21 @@ referencedClasses: []
 }),
 smalltalk.String);
 
+smalltalk.addMethod(
+'_join_',
+smalltalk.method({
+selector: 'join:',
+category: 'split join',
+fn: function (aCollection){
+var self=this;
+return smalltalk.send(smalltalk.String, "_streamContents_", [(function(stream){return smalltalk.send(aCollection, "_do_separatedBy_", [(function(each){return smalltalk.send(stream, "_nextPutAll_", [smalltalk.send(each, "_asString", [])]);}), (function(){return smalltalk.send(stream, "_nextPutAll_", [self]);})]);})]);
+return self;},
+source: unescape('join%3A%20aCollection%20%0A%09%5E%20String%0A%09%09streamContents%3A%20%5B%3Astream%20%7C%20aCollection%0A%09%09%09%09do%3A%20%5B%3Aeach%20%7C%20stream%20nextPutAll%3A%20each%20asString%5D%20%0A%09%09%09%09separatedBy%3A%20%5Bstream%20nextPutAll%3A%20self%5D%5D'),
+messageSends: ["streamContents:", "do:separatedBy:", "nextPutAll:", "asString"],
+referencedClasses: [smalltalk.String]
+}),
+smalltalk.String);
+
 
 smalltalk.addMethod(
 '_streamClass',
@@ -4556,6 +4571,24 @@ referencedClasses: []
 }),
 smalltalk.String.klass);
 
+smalltalk.addMethod(
+'_streamContents_',
+smalltalk.method({
+selector: 'streamContents:',
+category: 'instance creation',
+fn: function (blockWithArg){
+var self=this;
+var stream=nil;
+stream=smalltalk.send(smalltalk.send(self, "_streamClass", []), "_on_", [smalltalk.send(smalltalk.String, "_new", [])]);
+smalltalk.send(blockWithArg, "_value_", [stream]);
+return smalltalk.send(stream, "_contents", []);
+return self;},
+source: unescape('streamContents%3A%20blockWithArg%0A%09%7Cstream%7C%0A%09stream%20%3A%3D%20%28self%20streamClass%20on%3A%20String%20new%29.%0A%09blockWithArg%20value%3A%20stream.%0A%09%5E%20stream%20contents'),
+messageSends: ["on:", "streamClass", "new", "value:", "contents"],
+referencedClasses: [smalltalk.String]
+}),
+smalltalk.String.klass);
+
 
 smalltalk.addClass('Array', smalltalk.SequenceableCollection, [], 'Kernel');
 smalltalk.addMethod(
@@ -6253,6 +6286,21 @@ referencedClasses: [smalltalk.String]
 }),
 smalltalk.StringStream);
 
+smalltalk.addMethod(
+'_space',
+smalltalk.method({
+selector: 'space',
+category: 'writing',
+fn: function (){
+var self=this;
+smalltalk.send(self, "_nextPut_", [" "]);
+return self;},
+source: unescape('space%0A%09self%20nextPut%3A%20%27%20%27'),
+messageSends: ["nextPut:"],
+referencedClasses: []
+}),
+smalltalk.StringStream);
+
 
 
 smalltalk.addClass('ClassCommentReader', smalltalk.Object, ['class', 'chunkParser'], 'Kernel');

+ 15 - 0
js/SUnit.deploy.js

@@ -166,6 +166,21 @@ referencedClasses: []
 }),
 smalltalk.TestCase);
 
+smalltalk.addMethod(
+'_assert_equals_',
+smalltalk.method({
+selector: 'assert:equals:',
+category: 'testing',
+fn: function (expected, actual){
+var self=this;
+return smalltalk.send(self, "_assert_", [smalltalk.send(expected, "__eq", [actual])]);
+return self;},
+source: unescape('assert%3A%20expected%20equals%3A%20actual%0A%09%5E%20self%20assert%3A%20%28expected%20%3D%20actual%29'),
+messageSends: ["assert:", unescape("%3D")],
+referencedClasses: []
+}),
+smalltalk.TestCase);
+
 
 
 smalltalk.addClass('ExampleTest', smalltalk.TestCase, ['test'], 'SUnit');

+ 15 - 0
js/SUnit.js

@@ -166,6 +166,21 @@ referencedClasses: []
 }),
 smalltalk.TestCase);
 
+smalltalk.addMethod(
+'_assert_equals_',
+smalltalk.method({
+selector: 'assert:equals:',
+category: 'testing',
+fn: function (expected, actual){
+var self=this;
+return smalltalk.send(self, "_assert_", [smalltalk.send(expected, "__eq", [actual])]);
+return self;},
+source: unescape('assert%3A%20expected%20equals%3A%20actual%0A%09%5E%20self%20assert%3A%20%28expected%20%3D%20actual%29'),
+messageSends: ["assert:", unescape("%3D")],
+referencedClasses: []
+}),
+smalltalk.TestCase);
+
 
 
 smalltalk.addClass('ExampleTest', smalltalk.TestCase, ['test'], 'SUnit');

+ 1 - 0
js/jtalk.js

@@ -47,6 +47,7 @@
 	loadJS("SUnit.js");
 	loadJS("Examples.js");
 	loadJS("Benchfib.js");
+	loadJS("Kernel-Tests.js");
 
 	if(files) {
 	    for(var i=0; i < files.length; i++) {

+ 5 - 0
st/JQuery.st

@@ -53,6 +53,11 @@ val
 
 val: aString
     self call: 'val' withArgument: aString
+!
+
+attrAt: aString put: anotherString
+    "Set the value of an attribute for the first element in the set of matched elements."
+    <self['@jquery'].attr(aString, anotherString)>
 ! !
 
 !JQuery methodsFor: 'css'!

+ 18 - 0
st/Kernel-Tests.st

@@ -0,0 +1,18 @@
+TestCase subclass: #StringTest
+	instanceVariableNames: ''
+	category: 'Kernel-Tests'!
+
+!StringTest methodsFor: 'tests'!
+
+testJoin
+	self assert: 'hello,world' equals: (',' join: #('hello' 'world'))
+!
+
+testStreamContents
+	self 
+		assert: 'hello world' 
+		equals: (String streamContents: [:aStream| aStream 
+                                                 					nextPutAll: 'hello'; space; 
+                                                 					nextPutAll: 'world'])
+! !
+

+ 20 - 0
st/Kernel.st

@@ -1521,6 +1521,15 @@ trimBoth: separators
     	^(self trimLeft: separators) trimRight: separators
 ! !
 
+!String methodsFor: 'split join'!
+
+join: aCollection 
+	^ String
+		streamContents: [:stream | aCollection
+				do: [:each | stream nextPutAll: each asString] 
+				separatedBy: [stream nextPutAll: self]]
+! !
+
 !String methodsFor: 'testing'!
 
 isString
@@ -1557,6 +1566,13 @@ crlf
 
 fromString: aString
 	    <return new self.fn(aString)>
+!
+
+streamContents: blockWithArg
+	|stream|
+	stream := (self streamClass on: String new).
+	blockWithArg value: stream.
+	^ stream contents
 ! !
 
 SequenceableCollection subclass: #Array
@@ -2209,6 +2225,10 @@ crlf
 
 lf
 	^self nextPutAll: String lf
+!
+
+space
+	self nextPut: ' '
 ! !
 
 Object subclass: #ClassCommentReader

+ 4 - 0
st/SUnit.st

@@ -66,6 +66,10 @@ assert: aBoolean
 
 deny: aBoolean
 	self assert: aBoolean not
+!
+
+assert: expected equals: actual
+	^ self assert: (expected = actual)
 ! !
 
 TestCase subclass: #ExampleTest