Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expose mocking/testing types in barback #12519

Closed
sigmundch opened this issue Aug 16, 2013 · 4 comments
Closed

expose mocking/testing types in barback #12519

sigmundch opened this issue Aug 16, 2013 · 4 comments
Labels
area-pkg type-enhancement A request for a change that isn't a bug

Comments

@sigmundch
Copy link
Member

It would be great to expose in barback some mock datatypes that can be used by developers to test transformers.

For example, I currently write test using something like:

 testPhases('single script', [[new InlineCodeExtractor()]], {
      'a|test.html':
          '<!DOCTYPE html><html><head>'
          '<script type="application/dart">main() { }</script>',
    }, {
      'a|test.html':
          '<!DOCTYPE html><html><head>'
          '<script type="application/dart" src="test.html.0.dart"></script>'
          '</head><body></body></html>',

      'a|test.html.0.dart':
          'main() { }',
    });

Internally this runs the typical pipeline of barback once (with a mock package provider), attaches listeners to display errors if they occur, and checks the final outputs at the end.

To do this it was useful to have
 - a mock package provider
 - a pipeline helper that runs and check things

Here is some example code I have used so far:

testPhases(String testName, List<List<Transformer>> phases,
    Map<String, String> inputFiles, Map<String, String> expectedFiles) {
  test(testName, () {
    var helper = new TestHelper(phases, inputFiles)..run();
    return helper.checkAll(expectedFiles).then((_) => helper.tearDown());
  });
}

class TestPackageProvider implements PackageProvider {

  final List<List<Transformer>> transformers;

  // Maps from an asset string identifier of the form 'package|path' to the
  // file contents.
  final Map<String, String> files;
  final Iterable<String> packages;

  TestPackageProvider(this.transformers, files)
      : files = files,
        packages = files.keys.map((s) => idFromString(s).package);

  Future<Asset> getAsset(AssetId id) =>
      new Future.value(new Asset.fromString(id, files[idToString(id)]));

  Iterable<Iterable<Transformer>> getTransformers(String package) =>
      transformers;
}

String idToString(AssetId id) => '${id.package}|${id.path}';

AssetId idFromString(String s) {
  int index = s.indexOf('|');
  return new AssetId(s.substring(0, index), s.substring(index + 1));
}

class TestHelper {
  final Map<String, String> files;
  Barback barback;
  var errorSubscription;
  var resultSubscription;

  TestHelper(List<List<Transformer>> transformers, Map<String, String> files)
      : files = files,
        barback = new Barback(new TestPackageProvider(transformers, files)) {
    errorSubscription = barback.errors.listen((e) {
      fail('error running barback: $e');
    });
    resultSubscription = barback.results.listen((result) {
      expect(result.succeeded, isTrue, reason: "${result.errors}");
    });
  }

  void tearDown() {
    errorSubscription.cancel();
    resultSubscription.cancel();
  }

  // Tells barback which files have changed, and thus anything that depends on
  // it on should be computed. By default mark all the input files.
  void run([Iterable<String> paths]) {
    if (paths == null) paths = files.keys;
    barback.updateSources(paths.map(idFromString));
  }

  Future<String> operator [](String assetString){
    return barback.getAssetById(idFromString(assetString))
        .then((asset) => asset.readAsString());
  }

  Future check(String assetIdString, String content) {
    return this[assetIdString].then((value) {
      expect(value, content, reason: 'Final output of $assetIdString differs.');
    });
  }

  Future checkAll(Map<String, String> files) {
    var futures = [];
    files.forEach((k, v) {
      futures.add(check(k, v));
    });
    return Future.wait(futures);
  }
}

@sigmundch
Copy link
Member Author

Issue #12520 has been merged into this issue.

@sigmundch
Copy link
Member Author

Removed barbacn label.
Added Library-Barback label.

@anders-sandholm
Copy link
Contributor

Removed Library-Barback label.
Added Pkg-Barback label.

@DartBot
Copy link

DartBot commented Jun 5, 2015

This issue has been moved to dart-archive/barback#9.

@DartBot DartBot closed this as completed Jun 5, 2015
@kevmoo kevmoo added type-enhancement A request for a change that isn't a bug and removed triaged labels Mar 1, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-pkg type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

4 participants