Skip to content

Instantly share code, notes, and snippets.

@ochafik
Last active August 29, 2015 14:26
Show Gist options
  • Select an option

  • Save ochafik/e647ec8bce47f90aa9b6 to your computer and use it in GitHub Desktop.

Select an option

Save ochafik/e647ec8bce47f90aa9b6 to your computer and use it in GitHub Desktop.
import 'package:dart_jvm/dart_jvm.dart';
void main() async {
await JVM.create();
var map = java.util.HashMap();
map["Foo"] = "Bar";
print(map.size());
/// nCopies can be lifted as a Function
var nCopies = java.util.Collections.nCopies;
var hodors = nCopies(3, "Hodor");
print(hodors); // [Hodor, Hodor, Hodor]
List<String> hodorsDartList = JavaObject.dartify(hodors);
}
JavaClass.forName('com/google/dart/SomeClass');
assert(s.indexOf('d') >= 0);
// | | |
// | -1 false
// 'abc'
assert(() {
var value0 = s;
var value1 = 'd';
var invocation0 = value0.indexOf(value1);
var value2 = 0;
var binary0 = invocation0 >= value2;
if (!binary0) {
throwDiagrammedAssertionError('s.indexOf(\\'d\\') >= 0',
[value0, 0, invocation0, 2, binary0, 15]);
}
return true;
});
var a = 1, b = 2, c = 3, d = 4;
assert(a == b || c >= d)
// | | | | | | |
// 1 | 2 | 3 | 4
// | | false
// | false
// false
var o = {'a': {'b': {'c': 10}}};
assert(o['a'] == 11)
// | | |
// | | false
// | {"b":{"c":10}}
// {"a":{"b":{"c":10}}}
.used {}
.unused {}
.maybe-used
absent {}
present {}
<present class="used maybe-{{classSuffix}}">
</present>
import 'package:di/di.dart';
import 'package:ads.adsense.fe.shared.immutables/immutables.dart';
import 'package:unittest/unittest.dart';
class Foo {
int bar = 0;
List baz;
increment() { bar++; }
}
/// Make sure wrapped immutable [Foo] instances implement [Foo]
/// (useful in checked mode).
class _Foo extends Immutable implements Foo {
_Foo(target, immutables) : super(target, immutables);
noSuchMethod(i) => super.noSuchMethod(i);
}
main() {
test('example', () {
final injector = new ModuleInjector([
new ImmutablesModule()..bindWrappers({
Foo: (t, i) => new _Foo(t, i),
}),
]);
final immutables = injector.get(Immutables);
// Note: `final immutables = new Immutables();` uses a module with
// default configuration.
final foo = immutables.wrap(new Foo()..bar = 1..baz = [2, 3]);
expect(foo.bar, 1);
expect(() => foo.bar = 10, throws);
expect(foo.baz, [2, 3]);
expect(foo.baz.where((v) => v % 2 == 0).toList(), [2]);
expect(() => foo.baz = [], throws);
expect(() => foo.baz.clear(), throws);
});
}
log(String message, {bool loud: false}) {
print('[${loud ? 'SHOUT' : 'INFO'}]: $message');
}
...
main() {
log('Hello', loud: true);
}
class Foo {
log(String message, {bool loud: false}) {
print('[${loud ? 'SHOUT' : 'INFO'}]: $message');
}
}
main() {
new Foo().log('Hello', loud: true);
}
/**
* @param {string} message
* @param {{loud: ?boolean}=} opt_namedArgs
*/
var log = function(message, opt_namedArgs) {
var loud = opt_namedArgs && opt_namedArgs.loud;
if (!goog.isDef(loud)) loud = false;
dart.runtime.print(dart.runtime.interpolate("[", dart.runtime.notNullBoolean(loud) ? "SHOUT" : "INFO", "]: ", message));
};
...
log("Hello", {loud: true});
class Foo {
noSuchMethod(Invocation i) {
print('called ${i.memberName} '
'with ${i.positionalArguments} '
'and ${i.namedArguments}');
}
}
new Foo().bar(1, x: 2);
called Symbol("bar") with [1] and {Symbol("x"): 2}
render: function() {
return <div>Hello {this.props.name}</div>;
}
render: function() {
return React.createElement("div", null, "Hello ", this.props.name);
}
@Component(selector: "todos")
@View(
directives: const [For],
template: """
<ul>
<li *for="#todo of todos">
<input type="checkbox" [checked]="todo.done">{{todo.text}}
</li>
</ul>
<input type="text" #newtext>
<button (click)="addTodo(newtext.value)">Add</button>
<br/>
<button (click)="clearDone()">Clear</button>
<button (click)="allDone()">All</button>
<button (click)="noneDone()">None</button>
<button (click)="invertDone()">Invert</button>
""")
class Todos {
final List<Todo> todos = <Todo>[new Todo()..text = "first", new Todo()..text = "second"];
addTodo(String text) {
todos.add(new Todo()..text = text);
}
clearDone() => todos.removeWhere((Todo t) => t.done);
allDone() => todos.forEach((Todo t) => t.done = true);
noneDone() => todos.forEach((Todo t) => t.done = false);
invertDone() => todos.forEach((Todo t) => t.done = !t.done);
}
@Injectable()
class Todos extends regular.CanBuildRoot<Todos> {
@override
html.Node buildRoot(regular.Scope<Todos> scope$1) {
html.BodyElement body$1;
html.ButtonElement button$5;
html.ButtonElement button$4;
html.ButtonElement button$3;
html.ButtonElement button$2;
html.Node br$1;
html.ButtonElement button$1;
html.InputElement newtext;
html.Node ul$1;
html.Node li$for$1;
li$for$1 = scope$1.forElement((todo) {
html.Node li$1;
html.InputElement input$1;
input$1 = scope$1.createElement(tagName: "input", attributes: {
"type": "checkbox",
"checked": scope$1.observe(() => todo.done, setter: ($value) => todo.done = $value, watchName: "todo.done")
});
li$1 = scope$1.createElement(tagName: "li", content: [
"\n ",
input$1,
scope$1.observe(() => todo.text, watchName: "todo.text"),
"\n "
]);
return li$1;
}, scope$1.observe(() => scope$1.target.todos, watchName: "todo of todos"));
ul$1 = scope$1.createElement(tagName: "ul", content: [
"\n ",
li$for$1,
"\n "
]);
newtext = scope$1.createElement(tagName: "input", attributes: {
"type": "text"
});
button$1 = scope$1.createElement(tagName: "button", events: {
"click": (html.Event $event) => scope$1.target.addTodo(newtext.value)
}, content: [
"Add"
]);
br$1 = scope$1.createElement(tagName: "br");
button$2 = scope$1.createElement(tagName: "button", events: {
"click": (html.Event $event) => scope$1.target.clearDone()
}, content: [
"Clear"
]);
button$3 = scope$1.createElement(tagName: "button", events: {
"click": (html.Event $event) => scope$1.target.allDone()
}, content: [
"All"
]);
button$4 = scope$1.createElement(tagName: "button", events: {
"click": (html.Event $event) => scope$1.target.noneDone()
}, content: [
"None"
]);
button$5 = scope$1.createElement(tagName: "button", events: {
"click": (html.Event $event) => scope$1.target.invertDone()
}, content: [
"Invert"
]);
body$1 = scope$1.createElement(tagName: "body", content: [
ul$1,
"\n ",
newtext,
"\n ",
button$1,
"\n ",
br$1,
"\n ",
button$2,
"\n ",
button$3,
"\n ",
button$4,
"\n ",
button$5,
"\n "
]);
return body$1;
}
final List<Todo> todos = <Todo>[new Todo()..text = "first", new Todo()..text = "second"];
addTodo(String text) {
todos.add(new Todo()..text = text);
}
clearDone() => todos.removeWhere((Todo t) => t.done);
allDone() => todos.forEach((Todo t) => t.done = true);
noneDone() => todos.forEach((Todo t) => t.done = false);
invertDone() => todos.forEach((Todo t) => t.done = !t.done);
}
message MyRequest {
option (client.message_visibility) = PUBLIC;
optional string top_secret_name = 1;
optional string name = 2 [(client.field_visibility) = WHITELISTED];
}
enum Experiment {
EXPERIMENT_A = 1 [(client.value_visibility) = WHITELISTED];
TOP_SECRET_FUTURE_EXPERIMENT = 2;
}
expect(value, isNotNegative;
expect(value, isNot(contains("What?")));
expect(value, allOf(startsWith("prefix"), endsWith("suffix")));
assertThat(string).startsWith("foo");
assertThat(foo.isBar()).isTrue();
assertThat(item).isIn(collection);
assert( string.startsWith("foo") );
assert( value >= 0 );
assert( !value.contains("What?") );
assert( value.startsWith("prefix") && value.endsWith("suffix") );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment