From 4595ab7a3198df31fc1900ce5b3ac221aa935775 Mon Sep 17 00:00:00 2001 From: Steven Hugg Date: Sat, 30 Jun 2018 08:26:41 -0600 Subject: [PATCH] working on tests :^P --- doc/notes.txt | 1 + src/project.js | 13 ++-- src/store.js | 14 +++-- test/cli/teststore.js | 131 ++++++++++++++++++++++++++++++++++++++--- test/cli/testworker.js | 1 + 5 files changed, 141 insertions(+), 19 deletions(-) diff --git a/doc/notes.txt b/doc/notes.txt index bc49385d..98a6b472 100644 --- a/doc/notes.txt +++ b/doc/notes.txt @@ -32,6 +32,7 @@ TODO: - PC x86 support - show errors in list - can't see 1st line in editor sometimes +- online help WEB WORKER FORMAT diff --git a/src/project.js b/src/project.js index bf1ef0bb..ee27d46d 100644 --- a/src/project.js +++ b/src/project.js @@ -24,7 +24,7 @@ function SourceFile(lines, text) { } return 0; } - this.lineCount = function() { return this.line2offset.length; } + this.lineCount = function() { return lines.length; } } function CodeProject(worker, platform_id, platform, store) { @@ -44,7 +44,8 @@ function CodeProject(worker, platform_id, platform, store) { tools_preloaded[tool] = true; } } - + + // TODO: get local file as well as presets? self.loadFiles = function(filenames, callback) { var result = []; function loadNext() { @@ -63,7 +64,7 @@ function CodeProject(worker, platform_id, platform, store) { loadNext(); } else { var webpath = "presets/" + platform_id + "/" + fn; - if (platform_id == 'vcs' && webpath.indexOf('.') <= 0) + if (platform_id == 'vcs' && fn.indexOf('.') <= 0) webpath += ".a"; // legacy stuff $.get( webpath, function( text ) { console.log("GET",webpath,text.length,'bytes'); @@ -126,9 +127,9 @@ function CodeProject(worker, platform_id, platform, store) { self.updateFile = function(path, text, isBinary) { updateFileInStore(path, text); // TODO: isBinary - preloadWorker(path); if (okToSend()) { self.callbackBuildStatus(true); + preloadWorker(path); loadFileDependencies(text, function(depends) { worker.postMessage({ code:text, @@ -140,7 +141,7 @@ function CodeProject(worker, platform_id, platform, store) { } }; - function processBuildResult(data) { + self.processBuildResult = function(data) { if (data.listings) { for (var lstname in data.listings) { var lst = data.listings[lstname]; @@ -161,7 +162,7 @@ function CodeProject(worker, platform_id, platform, store) { } self.callbackBuildStatus(false); if (e.data && !e.data.unchanged) { - processBuildResult(e.data); + self.processBuildResult(e.data); self.callbackBuildResult(e.data); // call with data when changed } }; diff --git a/src/store.js b/src/store.js index 2ad778ec..57d49a7f 100644 --- a/src/store.js +++ b/src/store.js @@ -22,7 +22,7 @@ var OldFileStore = function(storage, prefix) { } this.deleteFile = function(name) { storage.removeItem(prefix + name); - storage.removeItem(prefix + 'local/' + name); + storage.removeItem(prefix + 'local/' + name); //TODO? } } @@ -63,7 +63,7 @@ var OldFileStoreDriver = { localforage.defineDriver(OldFileStoreDriver); // copy localStorage to new driver -function copyFromOldStorageFormat(platformid, newstore) { +function copyFromOldStorageFormat(platformid, newstore, callback) { var alreadyMigratedKey = "__migrated_" + platformid; //localStorage.removeItem(alreadyMigratedKey); if (localStorage.getItem(alreadyMigratedKey)) @@ -88,10 +88,14 @@ function copyFromOldStorageFormat(platformid, newstore) { migrateNext(); } else { newstore.length(function(err, len) { + if (err) throw err; console.log("Migrated " + len + " local files to new data store"); if (len) { localStorage.setItem(alreadyMigratedKey, 'true'); - window.location.reload(); + if (callback) + callback(); + else + window.location.reload(); } }); } @@ -101,11 +105,11 @@ function copyFromOldStorageFormat(platformid, newstore) { migrateNext(); // start the conversion } -function createNewPersistentStore(platformid) { +function createNewPersistentStore(platformid, callback) { var store = localforage.createInstance({ name: platformid, version: "2.0" }); - copyFromOldStorageFormat(platformid, store); + copyFromOldStorageFormat(platformid, store, callback); return store; } diff --git a/test/cli/teststore.js b/test/cli/teststore.js index 0fff9932..559623a5 100644 --- a/test/cli/teststore.js +++ b/test/cli/teststore.js @@ -8,25 +8,140 @@ var includeInThisContext = function(path) { vm.runInThisContext(code, path); }; -includeInThisContext("localForage/dist/localforage.nopromises.js"); -includeInThisContext("src/store.js"); - -var testplatform = "__TEST__"; - var localItems = {}; +var localMods = 0; global.localStorage = { + clear: function() { + localItems = {}; + localMods = 0; + this.length = 0; + }, getItem: function(k) { + console.log('get',k); return localItems[k]; }, setItem: function(k,v) { + console.log('set',k,v); + if (!localItems[k]) this.length++; localItems[k] = v; + localMods++; + }, + removeItem: function(k) { + if (localItems[k]) { + this.length--; + delete localItems[k]; + localMods++; + } + }, + length: 0, + key: function(i) { + var keys = []; + for (var k in localItems) + keys.push(k); + console.log(i,keys[i]); + return keys[i]; } }; +includeInThisContext("localForage/dist/localforage.js"); +includeInThisContext("src/store.js"); +includeInThisContext("src/project.js"); + +var test_platform_id = "_TEST"; + describe('Store', function() { - it('Should create persistent store', function() { - var store = createNewPersistentStore(testplatform); - // TODO + it('Should convert from local storage', function(done) { + localStorage.clear(); + localStorage.setItem('_TEST/test', 'a'); + localStorage.setItem('_TEST/local/test', 'b'); + assert.equal(2, localMods); + var store = createNewPersistentStore(test_platform_id, function() { + assert.equal('true', localItems['__migrated__TEST']); + store.getItem('test', function(err, result) { + if (err) done(err); + assert.equal(result, 'a'); + assert.equal(7, localMods); + done(); + }); + }); }); + + it('Should load local project', function(done) { + localStorage.clear(); + localStorage.setItem('_TEST/test', 'a'); + var store = createNewPersistentStore(test_platform_id, function() { + var worker = {}; + var platform = {}; + var project = new CodeProject(worker, test_platform_id, platform, store); + project.loadFiles(['test'], function(err, result) { + assert.equal(null, err); + assert.deepEqual([ { path: 'test', data: 'a' } ], result); + done(); + }); + }); + }); + + it('Should build project', function(done) { + localStorage.clear(); + localItems['__migrated__TEST'] = 'true'; + var msgs = []; + var expectmsgs = [ + true, + { preload: 'dasm', platform: '_TEST' }, + { code: ' lda #0', + dependencies: [], + platform: '_TEST', + tool: 'dasm' } + ]; + var store = createNewPersistentStore(test_platform_id); + var worker = { + postMessage: function(m) { msgs.push(m); }, + }; + var platform = { + getToolForFilename: function(fn) { return 'dasm'; }, + }; + var project = new CodeProject(worker, test_platform_id, platform, store); + project.callbackBuildStatus = function(b) { msgs.push(b) }; + project.updateFile('test.a', ' lda #0'); + project.updateFile('test.a', ' lda #1'); // don't send twice (yet) + assert.deepEqual(msgs, expectmsgs); + store.getItem('test.a', function(err, result) { + assert.equal(null, err); + assert.equal(' lda #1', result); + done(); + }); + }); + + // lines: [ { line: 3, offset: 61440, insns: 'a9 00', iscode: true } ] } + + it('Should build project', function(done) { + localStorage.clear(); + localItems['__migrated__TEST'] = 'true'; + var msgs = []; + var store = createNewPersistentStore(test_platform_id); + var worker = { + }; + var platform = { + }; + var project = new CodeProject(worker, test_platform_id, platform, store); + project.callbackBuildStatus = function(b) { msgs.push(b) }; + var buildresult = { + listings: { + test: { + lines: [ { line: 3, offset: 61440, insns: 'a9 00', iscode: true } ] + } + } + }; + worker.onmessage({data:buildresult}); + assert.deepEqual([false], msgs); + var lst = buildresult.listings.test; + console.log(lst); + assert.equal(3, lst.sourcefile.findLineForOffset(61440+15)); + assert.equal(0, lst.sourcefile.findLineForOffset(61440+16)); + assert.equal(0, lst.sourcefile.findLineForOffset(61440-1)); + assert.equal(1, lst.sourcefile.lineCount()); + done(); + }); + }); diff --git a/test/cli/testworker.js b/test/cli/testworker.js index 1bea93cb..d6a9110a 100644 --- a/test/cli/testworker.js +++ b/test/cli/testworker.js @@ -31,6 +31,7 @@ function doBuild(msgs, callback, outlen, nlines, nerrors) { var i = 0; for (var key in msg.listings) { var listing = msg.listings[key]; + //console.log(listing); assert.equal(listing.lines.length, nlines[i++], "listing lines"); } }