1
0
mirror of https://github.com/sehugg/8bitworkshop.git synced 2024-11-25 18:33:11 +00:00

working on tests :^P

This commit is contained in:
Steven Hugg 2018-06-30 08:26:41 -06:00
parent d146a7adee
commit 4595ab7a31
5 changed files with 141 additions and 19 deletions

View File

@ -32,6 +32,7 @@ TODO:
- PC x86 support - PC x86 support
- show errors in list - show errors in list
- can't see 1st line in editor sometimes - can't see 1st line in editor sometimes
- online help
WEB WORKER FORMAT WEB WORKER FORMAT

View File

@ -24,7 +24,7 @@ function SourceFile(lines, text) {
} }
return 0; return 0;
} }
this.lineCount = function() { return this.line2offset.length; } this.lineCount = function() { return lines.length; }
} }
function CodeProject(worker, platform_id, platform, store) { function CodeProject(worker, platform_id, platform, store) {
@ -45,6 +45,7 @@ function CodeProject(worker, platform_id, platform, store) {
} }
} }
// TODO: get local file as well as presets?
self.loadFiles = function(filenames, callback) { self.loadFiles = function(filenames, callback) {
var result = []; var result = [];
function loadNext() { function loadNext() {
@ -63,7 +64,7 @@ function CodeProject(worker, platform_id, platform, store) {
loadNext(); loadNext();
} else { } else {
var webpath = "presets/" + platform_id + "/" + fn; var webpath = "presets/" + platform_id + "/" + fn;
if (platform_id == 'vcs' && webpath.indexOf('.') <= 0) if (platform_id == 'vcs' && fn.indexOf('.') <= 0)
webpath += ".a"; // legacy stuff webpath += ".a"; // legacy stuff
$.get( webpath, function( text ) { $.get( webpath, function( text ) {
console.log("GET",webpath,text.length,'bytes'); console.log("GET",webpath,text.length,'bytes');
@ -126,9 +127,9 @@ function CodeProject(worker, platform_id, platform, store) {
self.updateFile = function(path, text, isBinary) { self.updateFile = function(path, text, isBinary) {
updateFileInStore(path, text); // TODO: isBinary updateFileInStore(path, text); // TODO: isBinary
preloadWorker(path);
if (okToSend()) { if (okToSend()) {
self.callbackBuildStatus(true); self.callbackBuildStatus(true);
preloadWorker(path);
loadFileDependencies(text, function(depends) { loadFileDependencies(text, function(depends) {
worker.postMessage({ worker.postMessage({
code:text, code:text,
@ -140,7 +141,7 @@ function CodeProject(worker, platform_id, platform, store) {
} }
}; };
function processBuildResult(data) { self.processBuildResult = function(data) {
if (data.listings) { if (data.listings) {
for (var lstname in data.listings) { for (var lstname in data.listings) {
var lst = data.listings[lstname]; var lst = data.listings[lstname];
@ -161,7 +162,7 @@ function CodeProject(worker, platform_id, platform, store) {
} }
self.callbackBuildStatus(false); self.callbackBuildStatus(false);
if (e.data && !e.data.unchanged) { if (e.data && !e.data.unchanged) {
processBuildResult(e.data); self.processBuildResult(e.data);
self.callbackBuildResult(e.data); // call with data when changed self.callbackBuildResult(e.data); // call with data when changed
} }
}; };

View File

@ -22,7 +22,7 @@ var OldFileStore = function(storage, prefix) {
} }
this.deleteFile = function(name) { this.deleteFile = function(name) {
storage.removeItem(prefix + 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); localforage.defineDriver(OldFileStoreDriver);
// copy localStorage to new driver // copy localStorage to new driver
function copyFromOldStorageFormat(platformid, newstore) { function copyFromOldStorageFormat(platformid, newstore, callback) {
var alreadyMigratedKey = "__migrated_" + platformid; var alreadyMigratedKey = "__migrated_" + platformid;
//localStorage.removeItem(alreadyMigratedKey); //localStorage.removeItem(alreadyMigratedKey);
if (localStorage.getItem(alreadyMigratedKey)) if (localStorage.getItem(alreadyMigratedKey))
@ -88,10 +88,14 @@ function copyFromOldStorageFormat(platformid, newstore) {
migrateNext(); migrateNext();
} else { } else {
newstore.length(function(err, len) { newstore.length(function(err, len) {
if (err) throw err;
console.log("Migrated " + len + " local files to new data store"); console.log("Migrated " + len + " local files to new data store");
if (len) { if (len) {
localStorage.setItem(alreadyMigratedKey, 'true'); 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 migrateNext(); // start the conversion
} }
function createNewPersistentStore(platformid) { function createNewPersistentStore(platformid, callback) {
var store = localforage.createInstance({ var store = localforage.createInstance({
name: platformid, name: platformid,
version: "2.0" version: "2.0"
}); });
copyFromOldStorageFormat(platformid, store); copyFromOldStorageFormat(platformid, store, callback);
return store; return store;
} }

View File

@ -8,25 +8,140 @@ var includeInThisContext = function(path) {
vm.runInThisContext(code, path); vm.runInThisContext(code, path);
}; };
includeInThisContext("localForage/dist/localforage.nopromises.js");
includeInThisContext("src/store.js");
var testplatform = "__TEST__";
var localItems = {}; var localItems = {};
var localMods = 0;
global.localStorage = { global.localStorage = {
clear: function() {
localItems = {};
localMods = 0;
this.length = 0;
},
getItem: function(k) { getItem: function(k) {
console.log('get',k);
return localItems[k]; return localItems[k];
}, },
setItem: function(k,v) { setItem: function(k,v) {
console.log('set',k,v);
if (!localItems[k]) this.length++;
localItems[k] = v; 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() { describe('Store', function() {
it('Should create persistent store', function() { it('Should convert from local storage', function(done) {
var store = createNewPersistentStore(testplatform); localStorage.clear();
// TODO 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();
});
}); });

View File

@ -31,6 +31,7 @@ function doBuild(msgs, callback, outlen, nlines, nerrors) {
var i = 0; var i = 0;
for (var key in msg.listings) { for (var key in msg.listings) {
var listing = msg.listings[key]; var listing = msg.listings[key];
//console.log(listing);
assert.equal(listing.lines.length, nlines[i++], "listing lines"); assert.equal(listing.lines.length, nlines[i++], "listing lines");
} }
} }