#!/bin/sh
# Exercise the parts of the SDK this package exists for: the VM, the AOT
# compiler, dart2js, the analyzer and the formatter.
set -eu

cd "${AUTOPKGTEST_TMP:-.}"

dart --version

cat > hello.dart <<'DART'
void main() => print('hello from dart');
DART

echo "--- dart run (VM/JIT)"
dart run hello.dart | grep -qx 'hello from dart'

echo "--- dart analyze"
dart analyze hello.dart

echo "--- dart format"
dart format --output=none hello.dart

echo "--- dart compile exe (AOT)"
dart compile exe hello.dart -o hello
./hello | grep -qx 'hello from dart'

echo "--- dart compile js (dart2js)"
dart compile js -o hello.js hello.dart
test -s hello.js

# dart doc exercises the dartdoc resource manifest, which Debian patches to drop
# the vendored minified Highlight.js. If that patch ever goes out of step with
# the excluded file, dartdoc fails here rather than in a user's documentation.
echo "--- dart doc"
mkdir -p pkgdemo/lib
cat > pkgdemo/pubspec.yaml <<'YAML'
name: pkgdemo
environment:
  sdk: ^3.0.0
YAML
cat > pkgdemo/lib/pkgdemo.dart <<'DART'
/// A documented library.
library;

/// Adds [a] and [b].
int add(int a, int b) => a + b;
DART
(cd pkgdemo && dart doc --output ../pkgdemo-doc)
test -s pkgdemo-doc/index.html
! grep -rq highlight.pack.js pkgdemo-doc/

echo "PASS"
